1、标准时间
分为UTC与GMT
@Test
public void test01(){
//标准时间 UTC GMT
//1900-01-01 00:00:00
//1970-01-01 00:00:00
Date date = new Date();
//Thu Apr 14 14:27:31 CST 2022
System.out.println(date);
//1649917651024 1970-01-01 00:00:00到现在经过的时间毫秒数
long time01 = date.getTime();
System.out.println(time01);
//1649917651033 1970-01-01 00:00:00到现在经过的时间毫秒数,与上边有差别是因为程序从上边到这里运行现需要这些毫秒
long time02 = System.currentTimeMillis();
System.out.println(time02);
}
2、日期字符串
自定义一个简单的日期字符串格式
@Test
public void test02(){
//日期字符串
Date date = new Date();
StringBuffer dateTest = new StringBuffer();
//getYear是获取的从1900年开始到现在经过的年份
int year = date.getYear() + 1900;
dateTest.append(year + "年");
//getMonth是获取的从0-11,因此需要+1
int month = date.getMonth() + 1;
dateTest.append((month < 10 ? "0" + month:month) + "月");
//getDate
int dayOfMonth = date.getDate();
dateTest.append((dayOfMonth < 10 ? "0" + dayOfMonth:dayOfMonth) + "日");
int hours = date.getHours();
dateTest.append("\t" + (hours < 10 ? "0" + hours:hours) + "时");
int minutes = date.getMinutes();
dateTest.append((minutes < 10 ? "0" + minutes:minutes) + "分");
int seconds = date.getSeconds();
dateTest.append((seconds < 10 ? "0" + seconds:seconds) + "分");
//0-6,0为日
int dayOfWeek = date.getDay();
String weekDay = "星期";
switch (dayOfWeek){
case 0 -> weekDay += "日";
case 1 -> weekDay += "一";
case 2 -> weekDay += "二";
case 3 -> weekDay += "三";
case 4 -> weekDay += "四";
case 5 -> weekDay += "五";
default -> weekDay += "六";
}
dateTest.append(weekDay);
System.out.println(dateTest);
}
3、利用java自带的方法设置模板及输出,以及时区
@Test
public void test03(){
Date date = new Date();
//传入时间模板 年yyyy月MM日dd,时HH分mm秒ss m注意大小写
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//设置时区
//dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println(dateFormat.format(date));
//将对应的时间模板转化为时间字符串
String source = "1998-12-12 12-12-12";
try {
Date date1 = dateFormat.parse(source);
System.out.println(date1);
} catch (ParseException e) {
//模板格式不对可能会有异常
throw new RuntimeException(e);
}
}
4、将对应的时间模板转为时间字符串,以及时间的比较
@Test
public void test04(){
//传入时间模板 年yyyy月MM日dd,时HH分mm秒ss m注意大小写
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//将对应的时间模板转化为时间字符串
String text01 = "1998-12-12 12-12-12";
String text02 = "1998-11-12 12-12-12";
try {
Date date1 = dateFormat.parse(text01);
Date date2 = dateFormat.parse(text02);
//时间早晚的比较
boolean isBefore = date1.getTime()-date2.getTime() < 0 ? true:false;
boolean isAfter = date1.getTime()-date2.getTime() > 0 ? true:false;
System.out.println(isBefore);
System.out.println(isAfter);
System.out.println(date1.before(date2));
System.out.println(date1.after(date2));
} catch (ParseException e) {
//模板格式不对可能会有异常
throw new RuntimeException(e);
}
}
5、本地时间
@Test
public void test05(){
Date date = new Date();
System.out.println(date);
//转化成格林威治时间
System.out.println(date.toGMTString());
//本地时间,并会自动转化为相应模板
System.out.println(date.toLocaleString());
}
6、时间毫秒转日期
@Test
public void test06(){
//时间毫秒转日期
long time = System.currentTimeMillis();
//time -= 1000 * 60 * 60;
Date date = new Date(time);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println(dateFormat.format(date));
}