JDK 8 之前日期时间API
System类中的currentTimeMillis()
System类中的currentTimeMillis():用来获取系统当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差,成为时间戳
System.out.println(time);
java.util.Date类
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.getTime());//1579020887779
System.out.println(date1.toString());//Wed Jan 15 00:54:47 CST 2020
//构造器二:创建指定毫秒数的Date对象
Date date2 = new Date(1111111111);
System.out.println(date2.getTime());//1111111111
System.out.println(date2.toString());//Wed Jan 14 04:38:31 CST 1970
java.sql.Date类
java.sql.Date对应数据库中的日期类型的变量
java.sql.Date sqlDate1 = new java.sql.Date(123456789l);
System.out.println(sqlDate1.toString());//1970-01-02
java.util.Date对象与java.sql.Date对象的转换
java.util.Date --> java.sql.Date:
sql.Date作为util.Date的子类,可以直接赋值(多态)
java.sql.Date --> java.util.Date:
Date date1 = new Date();
java.sql.Date sqlDate1 = new java.sql.Date(date1.getTime());
System.out.println(sqlDate1);
SimpleDataFormat类
SimpleDataFormat的使用:对日期Date类的格式化和解析
格式化:日期 —>字符串
解析:字符串 —> 日期
SimpleDateFormat的实例化:一般按照指定的方式格式化和解析:调用带参的构造器
Date date = new Date();
//格式化
SimpleDateFormat sdf = new SimpleDateFormat("yy..yyyy.111.,hhhh:mm:sss");//20..2020.111.,0002:04:053
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");//2020.01.15 02:04:53
System.out.println(sdf.format(date));
System.out.println(sdf1.format(date));
//解析
Date date1 = sdf1.parse("2020.01.15 02:04:53");//要求字符串格式要与格式化时的格式一样,否则抛异常
System.out.println(date1.toString());//Wed Jan 15 02:04:53 CST 2020
Calendar类
Calendar是一个抽象类,主用用于完成日期字段之间相互操作的功能。
实例化:
方式一:创建其子类(GregorianCalendar的对象)
方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);//获取今天是这个月的第几天
System.out.println(days);//15
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//15
//set()
//calendar可变性
calendar.set(Calendar.DAY_OF_MONTH, 22);//将今天设置为这个月的第22天
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//22
//add()
calendar.add(Calendar.DAY_OF_MONTH, -3);//在今天这个时间上加上amount天(负数为减去)
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():日历类---> Date
Date date = calendar.getTime();
System.out.println(date);//Sun Jan 19 02:34:52 CST 2020
//setTime():Date ---> 日历类
Date date1 = new Date();
calendar.setTime(date1);//将date1的时间赋给calendar
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//15
JDK 8 新加入的日期时间API
前两代存在的问题
- 可变性:像日期和时间这样的类应该是不可变的。
- 偏移性:Date中的年份是从1900开始的,而月份都从0开始。
- 格式化:格式化只对Date用,Calendar则不行。
- 此外,它们也不是线程安全的;不能处理闰秒等。
LocalDate / LocalTime / LocalDateTime的使用
说明:
- 分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
- LocalDateTime相较于LocalDate、LocalTime,使用频率要高
- 类似于Calendar
常用方法
//now()创建对象
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2020-01-19
System.out.println(localTime);//17:54:44.508
System.out.println(localDateTime);//2020-01-19T17:54:44.509
//of()创建对象
LocalDateTime localDateTime1 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 10, 13, 4,54);
System.out.println(localDateTime1);//2020-01-19T17:54:44.508
System.out.println(localDateTime2);//2020-10-13T04:54
//withXxx():设置相关的属性
//体现不可变性
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(31);//体现不可变性
System.out.println(localDateTime);//2020-01-19T18:00:50.389
System.out.println(localDateTime1);//2020-01-31T18:00:50.389
Instant的使用
常用方法
Instant instant = Instant.now();
System.out.println(instant);//2020-01-19T13:44:05.529Z
//添加时间的偏移量,获取本地时间
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2020-01-19T21:44:05.529+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
System.out.println(instant.toEpochMilli());//1579441445529 毫秒数
System.out.println(offsetDateTime.toEpochSecond());//1579441445 秒数
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1579441445529l);
System.out.println(instant1);//2020-01-19T13:44:05.529Z
DateTimeFormat类
- 格式化或解析日期、时间
- 类似SimpleDateFormat
实例化方式
- 预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
- 本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
- 自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
常用方法
//方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);//2020-01-20T15:25:27.633
//格式化:日期-->字符串
String str1 = formatter.format(localDateTime);
System.out.println(str1);//2020-01-20
//解析:字符串-->日期
TemporalAccessor parse = formatter.parse(str1);
System.out.println(parse);//{},ISO resolved to 2020-01-20
//方式二:本地化相关的格式。如:ofLocalizedDateTime()
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//下午03时32分03秒
//解析
TemporalAccessor parse1 = formatter1.parse("上午12时56分24秒");
System.out.println(parse1);//{},ISO resolved to 00:56:24
//重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy,MM,dd hh:mm:ss");
//格式化
String str3 = formatter2.format(localDateTime);
System.out.println(str3);//2020,01,20 03:42:09
//解析
TemporalAccessor parse2 = formatter2.parse(str3);
System.out.println(parse2);//{NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=3, HourOfAmPm=3, MinuteOfHour=43, MilliOfSecond=0},ISO resolved to 2020-01-20