JAVA8 日期时间
在java8中,java.time包下主要包含下面几个主要的类:
Instant:时间戳,相当于java.util的Date
LocalDate:只包含日期,比如:2021-03-21
LocalTime:只包含时间,比如:12:06:47
LocalDateTime:包含日期和时间,比如:2021-03-21 12:06:50
Duration:计算两个“时间”的间隔
Period:用于计算两个“日期”的间隔
ZoneOffset:时区偏移量,比如:+12:00
ZonedDateTime:可以得到特定时区的日期/时间
Clock:时钟,比如获取目前美国纽约的时间
获取获取年、月、日、时…信息 :
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();//2021
System.out.println(year);
Month month = now.getMonth();//MARCH
int dayOfMonth = now.getDayOfMonth();//21
int hour = now.getHour();//16
int minute = now.getMinute();//8
int dayOfYear = now.getDayOfYear();//80
int second = now.getSecond();//32
日期增加、减少
LocalDateTime of = LocalDateTime.of(2020, 1, 1, 1, 1,1);
System.out.println(of);//2020-01-01T01:01:01
LocalDateTime nextYearTime = of.plusYears(1);
System.out.println(nextYearTime);//2021-01-01T01:01:01
LocalDateTime localDateTime = of.minusYears(1);
System.out.println(localDateTime);//2019-01-01T01:01:01
使用with方法设置月份
LocalDateTime of = LocalDateTime.of(2021, 2, 1, 1, 1,1);
LocalDateTime changeTime = of.withMonth(3);
System.out.println(changeTime);//2021-03-01T01:01:01
Duration : 计算两个“时间”的间隔
LocalDateTime from = LocalDateTime.of(2021, Month.JANUARY, 21, 15, 56, 0); // 2019-01-21 15:56:00
LocalDateTime to = LocalDateTime.of(2021, Month.FEBRUARY, 21, 15, 56, 0); // 2019-02-21 15:56:00
Duration duration = Duration.between(from, to); // 表示从 2021-01-21 15:56:00 到 2021-02-21 15:56:00
long days = duration.toDays(); // 这段时间的总天数
System.out.println(days);//31
long hours = duration.toHours(); // 这段时间的小时数
System.out.println(hours);//744
long minutes = duration.toMinutes(); // 这段时间的分钟数
System.out.println(minutes);//44640
long seconds = duration.getSeconds(); // 这段时间的秒数
System.out.println(seconds);//2678400
long milliSeconds = duration.toMillis(); // 这段时间的毫秒数
System.out.println(milliSeconds);//2678400000
long nanoSeconds = duration.toNanos(); // 这段时间的纳秒数
System.out.println(nanoSeconds);//2678400000000000
ZoneId : 时区
获取所有合法的“区域/城市”字符
Set<String> zoneIds = ZoneId.getAvailableZoneIds();//[Asia/Aden, America/Cuiaba, Etc/GMT+9,...]
判断两个日期是否相等
LocalDate of = LocalDate.of(2020, 01, 21);
System.out.println(of.equals(LocalDate.now()));//false
java8中的时间类与Date类的相互转化
Instant instant = Instant.now();
Date date = Date.from(instant);
Instant instant1 = date.toInstant();
System.out.println(instant1);//2021-03-21T09:00:41.950Z
Date date2 = new Date();
LocalDateTime localDateTime = LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault());
System.out.println(localDateTime);//2021-03-21T17:00:42.005
LocalDateTime now = LocalDateTime.now();
Instant instant2 = now.atZone(ZoneId.systemDefault()).toInstant();
Date date3 = Date.from(instant2);
System.out.println(date3);//Sun Mar 21 17:00:42 CST 2021
LocalDate now1 = LocalDate.now();
Instant instant3 = now1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date date4 = Date.from(instant3);
System.out.println(date4);//Sun Mar 21 00:00:00 CST 2021
日期的格式化和解析DateTimeFormatter
- Date转String
LocalDateTime now = LocalDateTime.now(); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("G yyyy年MM月dd号 E a hh时mm分ss秒"); String format = now.format(pattern); System.out.println(format);//公元 2021年03月21号 星期日 下午 05时04分14秒
- String转Date
-
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dt = LocalDateTime.parse("2021-03-21 17:15:30",pattern); System.out.println(dt.format(pattern));//2021-03-21 17:15:30
本文转载自JAVA8 日期时间API整理_李某乐的博客-CSDN博客 ,详细及更多内容请前往原著作者文章查看。