一、时间类
1、2个标准时间
UTC 1900-01-01 00:00:00
GMT 1970-01-01 00:00:00
2、输出时间
---输出现在的日期及时间
Date date = new Date();
System.out.println(date);
输出结果
Thu Apr 14 20:26:09 CST 2022
---输出从 1970-01-01 00:00:00 到程序运行的时间的毫秒数
long time = date.getTime();
System.out.println(time);
输出结果
1649939283871
---输出从 1970-01-01 00:00:00到程序运行的时间的毫秒数
long millis = System.currentTimeMillis();
System.out.println(millis);
输出结果
1649939412690
二、输出年、月、日、时、分、秒、星期等内容
public void test02() {
//日期字符串
StringBuilder dateText = new StringBuilder();
Date date = new Date();
//年
int year = date.getYear() + 1900;
dateText.append(year + "年");
//月
int month = date.getMonth() + 1;
dateText.append(month < 10 ? "0" + month + "月" : month + "月");
//日
int dayOfMonth = date.getDate();
dateText.append(dayOfMonth < 10 ? "0" + dayOfMonth + "日" : dayOfMonth + "日");
//时
int hours = date.getHours();
dateText.append(hours < 10 ? " 0" + hours + "时" : +hours + "时");
//分
int minutes = date.getMinutes();
dateText.append(minutes < 10 ? " 0" + minutes + "分" : +minutes + "分");
//秒
int seconds = date.getSeconds();
dateText.append(seconds < 10 ? " 0" + seconds + "分" : +seconds + "分");
//星期
int dayOfWeek = date.getDay();
String weekDay = " 星期";
switch (dayOfWeek) {
case 1 -> weekDay += "一";
case 2 -> weekDay += "二";
case 3 -> weekDay += "三";
case 4 -> weekDay += "四";
case 5 -> weekDay += "五";
case 6 -> weekDay += "六";
case 7 -> weekDay += "日";
}
dateText.append(weekDay);
//输出结果
System.out.println(dateText.toString());
}
输出结果
2022年4月14日 15时36分46秒 星期四
三、定义时间模板,输出标准时间
public void test03() {
Date date = new Date();
System.out.println(date);
//实例化一个时间字符串格式对象,并传入时间字符串模板
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/shanghai"));
//将日期对象根据之前的时间字符串模板按格式化为时间字符串
String format = dateFormat.format(date);
System.out.println(format);
String source = "1983-11-22 20:30:00";
try {
Date parse = dateFormat.parse(source);
System.out.println(parse);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
输出结果
Fri Apr 15 08:39:45 CST 2022
2022-04-15 00:39:45
Wed Nov 23 04:30:00 CST 1983
四、比较2个时间前后
public void test04() {
//实例化一个时间字符串格式对象,冰川乳时间字符串模板
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String text01 = "1983-11-22 20:30:00";
String text02 = "1988-08-11 20:30:00";
try {
Date date01 = dateFormat.parse(text01);
Date date02 = dateFormat.parse(text02);
boolean isBefore = date01.getTime() - date02.getTime() < 0 ? true : false;
boolean isAfter = date01.getTime() - date02.getTime() > 0 ? true : false;
System.out.println(isBefore);
System.out.println(isAfter);
System.out.println(date01.before(date02));
System.out.println(date01.after(date02));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
输出结果
true
false
true
false
五、时间表达方式
public void test05() {
Date date = new Date();
System.out.println(date);
String gmtString = date.toGMTString();
String localeString = date.toLocaleString();
System.out.println(localeString);
System.out.println(gmtString);
}
输出结果
Fri Apr 15 08:42:45 CST 2022
2022年4月15日 上午8:42:45
15 Apr 2022 00:42:45 GMT
六、不同时区
public void test06() {
//输出当前系统默认时
//1000
//1000*60
//1000*60*60
//1000*60*60*8
System.out.println(TimeZone.getDefault());
//定义时间字符串格式化模板
SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//格式化的时候设置时区
tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
//获取当前时间对象
Date tokyoDate = new Date();
//将当前时间对象使用定义好的时间字符串模板格式化
String tokyoDateText = tokyoSdf.format(tokyoDate);
System.out.println(tokyoDateText);
SimpleDateFormat NewYorkSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
NewYorkSdf.setTimeZone(TimeZone.getTimeZone("American/NewYork"));
Date NewYorkDate = new Date();
String newYorkDateText = NewYorkSdf.format(NewYorkDate);
System.out.println(newYorkDateText);
}
输出结果
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null]
2022-04-15 09:44:47
2022-04-15 00:44:47
七、输出未来时间
public void test07() {
long time = System.currentTimeMillis();
time -= 1000 * 60 * 60;
Date date = new Date();
System.out.println(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/shanghai"));
String format = dateFormat.format(date);
System.out.println(format);
}
输出结果
Fri Apr 15 08:45:36 CST 20222022-04-15 00:45:36