1.System类的currentTimeMillis()
①获取当前时间对应的毫秒数,long类型,时间戳
②当前时间与1970年1月1日0分0秒之间的毫秒数
③常用来计算时间差
1.java.util.Date
//创建一个基于当前系统时间的Date实例
Date date1=new Date();
//Mon Oct 23 21:31:54 CST 2023
System.out.print(date1.toString());
long milliTimes=date1.getTime();
//1698068033128
System.out.print("\n对应的毫秒数"+milliTimes);
//创建一个基于指定时间戳的Date的实例
Date date2=new Date(1698068033128L);
System.out.print("\n"+date2.toString());
2.java.sql.Date(对应数据库中Date)
java.sql.Date date1=new java.sql.Date(1698068033128L);
//2023-10-23
System.out.print(date1.toString());
//1698068033128
System.out.print(date1.getTime());
SimpleDateFormat类:用于时间日期的格式化和解析
格式化:日期—>字符串
解析:字符串—>日期
public void test3() throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat();
//格式化:日期to字符串
Date date1=new Date();
String strData = sdf.format(date1);
//23-10-23 下午9:48
System.out.print(strData);
//解析:字符串to日期
Date date2 = sdf.parse("23-10-23 下午9:48");
//Mon Oct 23 21:48:00 CST 2023
System.out.print(date2);
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date3=new Date();
String strData2=sdf2.format(date3);
//2023-10-23 21:54:13
System.out.print(strData2);
Date date4=sdf2.parse("2023-10-23 21:54:13");
//Mon Oct 23 21:54:13 CST 2023
System.out.print(date4);
}
4.java.util.Calendar(日历)
Date类的大部分Api被废弃了,替换为Calendar
public void test5(){
/**
* 实例化,由于Calendar是抽象类,所以我们需要创建其子类的实例
* 这里我们通过Calendar的静态方法
*/
Calendar calendar=Calendar.getInstance();
//get(int field)
//23,这一月的第多少天
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)+"\n");
//296,这一年的第多少天
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)+"\n");
//set(int field,xx)
calendar.set(Calendar.DAY_OF_MONTH,25);
//25
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
//add(int field,xx)
calendar.add(Calendar.DAY_OF_MONTH,3);
//28
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
//Calendar to Date
Date date=calendar.getTime();
//Sat Oct 28 22:11:21 CST 2023
System.out.println(date);
}
之前时间API存在的问题
可变性: 像日期和时间这样的类应该是不可变的
偏移性: Date中的年份是从1900开始的,而月份都是从0开始的
格式化: 格式化只对Date有用,Calendar则不行
此外,他们也不是线程安全的,不能处理闰秒等
为了解决问题引入新的API。
public void test6(){
LocalDate localDate=LocalDate.now();
LocalTime localTime= LocalTime.now();
LocalDateTime localDateTime=LocalDateTime.now();
//2023-10-24
System.out.println(localDate);
//21:58:26.449
System.out.println(localTime);
//2023-10-24T21:58:26.449
System.out.println(localDateTime);
//of()
LocalDate localDate1=LocalDate.of(2000,11,2);
LocalDateTime localDateTime1= LocalDateTime.of(2000,9,11,20,24,30);
//2000-11-02
System.out.println(localDate1);
//2000-09-11T20:24:30
System.out.println(localDateTime1);
//getXXX()
LocalDateTime localDateTime2=LocalDateTime.now();
//24
System.out.println(localDateTime2.getDayOfMonth());
//withXXX() 体现不可变性
LocalDateTime localDateTime3 = localDateTime2.withDayOfMonth(15);
//2023-10-15T22:06:25.410
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime2.plusDays(2);
//2023-10-26T22:07:38.908
System.out.println(localDateTime4);
}
时间线上的一个瞬时点,可能被用来记录应用程序的事件的事件戳,与Date有点类似
public void test7(){
Instant instant=Instant.now();
//2023-10-24T14:14:43.069Z
System.out.println(instant);
Instant instant1 = Instant.ofEpochMilli(121313213123L);
//1973-11-05T02:06:53.123Z
System.out.println(instant1);
long milli = instant.toEpochMilli();
//1698157087034
System.out.println(milli);
}
public void test8(){
//自定义格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime=LocalDateTime.now();
String format = dateTimeFormatter.format(localDateTime);
//2023-10-24 10:23:02
System.out.println(format);
//解析 字符串->日期时间
TemporalAccessor parse = dateTimeFormatter.parse("2023-10-24 10:23:02");
LocalDateTime from = LocalDateTime.from(parse);
//2023-10-24T10:23:02
System.out.println(from);
}