Commit 03a2e4ba authored by 时良荣's avatar 时良荣

宋欢 => Java培训 Date Math

parent c545c4cb
package com.exercise.seventh;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 写个时间工具类,包括4个方法:
* •获取当前时间戳(秒)
* •获取当前时间,yyyy-MM-dd HH:mm:ss格式的字符串
* •Date转yyyy-MM-dd HH:mm:ss格式的字符串
* •任意字符串转Date
* @author elliot
*/
public class ExerciseFifth {
public static void main(String[] args) throws ParseException {
long currentTimeString = TimeUtility.getCurrentTimeString();
String dateTime = TimeUtility.stampToDate(currentTimeString * 1000L);
String dateTimeNew = TimeUtility.dateToString();
Date newDate = TimeUtility.stringToDate(dateTimeNew);
System.out.println("获取当前时间戳(秒):" + currentTimeString);
System.out.println("获取当前时间,yyyy-MM-dd HH:mm:ss格式的字符串:" + dateTime);
System.out.println("Date转yyyy-MM-dd HH:mm:ss格式的字符串:" + dateTimeNew);
System.out.println("任意字符串转Date:" + newDate);
}
}
class TimeUtility {
/**
* 获取当前时间戳(秒)
* @return
*/
static long getCurrentTimeString() {
long currentTimeString = System.currentTimeMillis() / 1000L;
return currentTimeString;
}
/**
* 获取当前时间,yyyy-MM-dd HH:mm:ss格式的字符串
* @param s
* @return
*/
public static String stampToDate(long s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(s);
res = simpleDateFormat.format(date);
return res;
}
/**
* Date转yyyy-MM-dd HH:mm:ss格式的字符串
* @return
*/
public static String dateToString() {
Date date = new Date();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDate = formatter.format(date);
return currentDate;
}
/**
* 任意字符串转Date
* @param checkTime
* @return
* @throws ParseException
*/
public static Date stringToDate(String checkTime) throws ParseException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date result = formatter.parse(checkTime);
return result;
}
}
package com.exercise.seventh;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 宋欢 => Java培训 Date Math
* 你的上次生日过了多少天?下次生日还有多少天?
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args) throws ParseException {
String prevBirthday = "2020-05-05";
String nextBirthday = "2021-05-05";
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
String todayDate = formatter.format(new Date());
System.out.println("上次生日过了" + betweenDays(prevBirthday, todayDate) + "天");
System.out.println("下次生日还有" + betweenDays(todayDate, nextBirthday) + "天");
}
/**
* 计算两个日期之间相差的天数
*/
public static int betweenDays(String startDate, String endDate){
// 获取日期
Date date1 = parseDate(startDate, "yyyy-MM-dd");
Date date2 = parseDate(endDate, "yyyy-MM-dd");
// 获取相差的天数
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
long timeInMillis1 = calendar.getTimeInMillis();
calendar.setTime(date2);
long timeInMillis2 = calendar.getTimeInMillis();
long betweenDays = (timeInMillis2 - timeInMillis1) / (1000L*3600L*24L);
return (int) betweenDays;
}
/**
* 日期格式化
* @param dateStr
* @param pattern
* @return
*/
public static Date parseDate(String dateStr, String pattern)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date;
try {
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException("日期转化错误");
}
return date;
}
}
package com.exercise.seventh;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 写个方法,判断上/下午,boolean isAfternoon(String s),调用方式,如:isAfternoon("2006-01-02 03:04:05")
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args) throws ParseException {
// 自定义时间校验
String checkDateTime = "2020-09-26 13:04:05";
// 获取当前时间校验
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String checkDateTime2 = formatter.format(new Date());
// if (isAfternoon(checkDateTime)) {
if (isAfternoon(checkDateTime2)) {
System.out.println("现在是下午时间");
} else {
System.out.println("现在是上午时间");
}
}
static boolean isAfternoon(String checkTime) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(checkTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour <= 12) {
return false;
}
return true;
}
}
package com.exercise.seventh;
/**
* 用Random,生成1000个[0, 10)之间的随机数,记录每个数出现次数。如果0、1、2、3、4、5、6、7、8、9每个
* 数出现100次,说明Random生成的整数是均匀的。如果0出现200次,1出现0次,说明不均匀。
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args) {
int[] count = new int[10];
for (int i = 0; i < 1000; i++) {
double num1 = Math.random();
double num2 = num1 * 10;
int num3 = (int)num2;
count[num3] ++;
}
for (int i = 0; i < count.length; i++) {
System.out.println("数字【 " + i + " 】出现的次数是:" + count[i]);
}
}
}
package com.exercise.seventh;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 写个方法,字符串转Date,Date parse(String s),调用方式,如:
* •Date t1 = parse("2006-01-02 03:04:05")
* •Date t2 = parse("2006-01-02")
* •Date t3 = parse("2006/01/02 03:04:05")
* •Date t4 = parse("2006/01/02")
* •注1:不可增加日期格式参数,如:Date parse(String s, String pattern),pattern 传 yyyy-MM-dd HH:mm:ss
* •注2:可用三方工具类
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args) {
Date t1 = parse("2006-01-02 03:04:05");
Date t2 = parse("2006-01-02");
Date t3 = parse("2006/01/02 03:04:05");
Date t4 = parse("2006/01/02");
System.out.println(t1);
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
}
private static Date parse(String date) {
Pattern pattern = Pattern.compile("(?<year>\\d{4})[-/](?<month>\\d{2})[-/](?<date>\\d{2})( +(?<hour>\\d{2}):(?<minute>\\d{2}):(?<second>\\d{2}))?");
Matcher matcher = pattern.matcher(date);
if (matcher.find()) {
Calendar calendar = Calendar.getInstance();
int year = Integer.parseInt(matcher.group("year"));
int month = Integer.parseInt(matcher.group("month")) - 1;
int day = Integer.parseInt(matcher.group("date"));
int hour = 0;
int minute = 0;
int second = 0;
if (matcher.group("hour") != null) {
hour = Integer.parseInt(matcher.group("hour"));
}
if (matcher.group("minute") != null) {
minute = Integer.parseInt(matcher.group("minute"));
}
if (matcher.group("second") != null) {
second = Integer.parseInt(matcher.group("second"));
}
calendar.set(year, month, day, hour, minute, second);
return calendar.getTime();
} else {
throw new DateTimeException("解析日期时间错误");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment