计算机系统应用教程网站

网站首页 > 技术文章 正文

带有时区的字符怎样转换为时间及Java 8中日期 与 Calendar 转换

btikc 2024-10-29 13:14:19 技术文章 22 ℃ 0 评论

概述

Java 8 日期,时间常用操作及格式化。

重点怎么把时间戳转换为带有时区的时间字符串?

带有时区的字符串怎样转换为时间?

Java 8中时间,日期 与Date, Calendar 怎样互转?

Java 6,7中想使用Java 8中的日期处理,应该怎么解决?

Chrono Units Enum

一套标准的日期期间单位。

这套单元提供基于单元的访问来操作日期、时间或日期时间。单位的标准可以通过实施扩展

1. LocalDate date = LocalDate.now(); 
2. System.out.println("current date is :" + 
3. date); 
4. // adding 2 years to the current date 
5. LocalDate year = 
6. date.plus(2, ChronoUnit.YEARS); 
7. 
8. System.out.println("next to next year is " + 
9. year); 
10. // adding 1 month to the current data 
11. LocalDate nextMonth = 
12. date.plus(1, ChronoUnit.MONTHS); 
13. 
14. System.out.println("the next month is " + 
15. nextMonth); 
16. // adding 1 week to the current date 
17. LocalDate nextWeek = 
18. date.plus(1, ChronoUnit.WEEKS); 
19. 
20. System.out.println("next week is " + nextWeek); 
21. 
22. // adding 2 decades to the current date 
23. LocalDate Decade = 
24. date.plus(2, ChronoUnit.DECADES); 
25. 
26. System.out.println("20 years after today " + 
27. Decade);

输出结果

1. current date is :2018-04-09 
2. next to next year is 2020-04-09 
3. the next month is 2018-05-09 
4. next week is 2018-04-16 
5. 20 years after today 2038-04-09 

Temporal Adjusters

包含常用日期获取常量参数。

1. LocalDate date = LocalDate.now(); 
2. System.out.println("the current date is "+ 
3. date); 
4. 
5. // to get the first day of next month 
6. LocalDate dayOfNextMonth = 
7. date.with(TemporalAdjusters. 
8. firstDayOfNextMonth()); 
9. 
10. System.out.println("firstDayOfNextMonth : " + 
11. dayOfNextMonth ); 
12. 
13. // get the next saturday 
14. LocalDate nextSaturday = 
15. date.with(TemporalAdjusters. 
16. next(DayOfWeek.SATURDAY)); 
17. 
18. System.out.println("next satuday from now is "+ 
19. nextSaturday); 
20. 
21. // first day of current month 
22. LocalDate firstDay = 
23. date.with(TemporalAdjusters. 
24. firstDayOfMonth()); 
25. 
26. System.out.println("firstDayOfMonth : " + 
27. firstDay); 
28. 
29. // last day of current month 
30. LocalDate lastDay = 
31. date.with(TemporalAdjusters. 
32. lastDayOfMonth()); 
33. 
34. System.out.println("lastDayOfMonth : " + 
35. lastDay); 

常用方法

如果上面表格中列出的方法不能满足你的需求,你还可以创建自定义的TemporalAdjuster接口的实现,TemporalAdjuster也是一个函数式接口,所以我们可以使用Lambda表达式:

@FunctionalInterfacepublic interface TemporalAdjuster { Temporal adjustInto(Temporal temporal);}

计算下个工作日,跳过周六,周天。

1. LocalDate date = LocalDate.of(2019, 10, 11); 
2. LocalDateTime localDateTime2 = date.with(temporal -> { 
3. // 当前日期 
4. DayOfWeek dayOfWeek = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); 
5. 
6. // 正常情况下,每次增加一天 
7. int dayToAdd = 1; 
8. 
9. // 如果是星期五,增加三天 
10. if (dayOfWeek == DayOfWeek.FRIDAY) { 
11. dayToAdd = 3; 
12. } 
13. 
14. // 如果是星期六,增加两天 
15. if (dayOfWeek == DayOfWeek.SATURDAY) { 
16. dayToAdd = 2; 
17. } 
18. 
19. return temporal.plus(dayToAdd, ChronoUnit.DAYS); 
20. }).atStartOfDay(); 
21. System.out.println(localDateTime2); 
22. //结果 
23. 2019-10-14T00:00 

与旧日期兼容

Date, Calendar通过Instant转换为LocalDateTime

1. Date date = new Date(); 
2. Calendar calendar = Calendar.getInstance(); 
3. LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); 
4. System.out.println("date to localDateTime "+localDateTime); 
5. LocalDateTime localDateTime1= LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()); 
6. System.out.println("calendar to localDateTime "+localDateTime1); 

Instant 转化为 Date,Calendar

Instant 转换Date可以直接通过from 方法构建。

Instant 转换 Calendar,先转换ZonedDateTime,在使用GregorianCalendar from 方法。

1. Instant instant = Instant.now(); 
2. Date myDate = Date.from(instant); 
3. ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); 
4. Calendar cal1 = GregorianCalendar.from(zdt);

日期格式化

新的日期API中提供了一个DateTimeFormatter类用于处理日期格式化操作,它被包含在java.time.format包中,Java 8的日期类有一个format()方法用于将日期格式化为字符串,该方法接收一个DateTimeFormatter类型参数:

1. LocalDateTime dateTime = LocalDateTime.now(); 
2. String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); 
3. System.out.println("BASIC_ISO_DATE time"+strDate1); 
4. String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); 
5. System.out.println("ISO_LOCAL_DATE time"+strDate2); 
6. String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); 
7. System.out.println("ISO_LOCAL_TIME time"+strDate3); 
8. String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); 
9. System.out.println("yyyy-MM-dd time"+strDate4); 
10. String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:yyyy年 MM月 dd日 ", Locale.CHINESE)); 
11. System.out.println("Locale.CHINESE time"+strDate5); 

输出结果

1. BASIC_ISO_DATE time20191015 
2. ISO_LOCAL_DATE time2019-10-15 
3. ISO_LOCAL_TIME time15:01:26.614 
4. yyyy-MM-dd time2019-10-15 
5. Locale.CHINESE time今天是:2019年 10月 15日 

时间戳转换时间字符串(带有时区格式)

1. String s = Instant.ofEpochMilli(System.currentTimeMillis()). 
2. atZone(ZoneId.systemDefault()) 
3. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); 
4. System.out.println(s);

输出结果

5. 2019-10-15T15:03:30.995+0800

日期类也支持将一个字符串解析成一个日期对象,带有时区格式

1. String strDate6 = "2019-01-01"; 
2. String strDate7 = "2019-10-15T15:03:30.995+0800"; 
3. LocalDate date1 = LocalDate.parse(strDate6, DateTimeFormatter.ofPattern("yyyy-MM-dd")); 
4. LocalDateTime dateTime1 = LocalDateTime 
5. .parse(strDate7, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); 
6. System.out.println(date1); 
7. System.out.println(dateTime1);

输出结果

1. 2019-01-01 
2. 2019-10-15T15:03:30.995

第三方日期工具

threetenbp Backport of JSR-310 from JDK 8 to JDK 7 and JDK 6. NOT an implementation of the JSR.

1. <dependency> 
2. <groupId>org.threeten</groupId> 
3. <artifactId>threetenbp</artifactId> 
4. <version>1.4.0</version> 
5. </dependency> 

Joda-Time是一个出来日期时间的库,JDK1.8的API,引入的java.time包,其实也是借鉴了Joda-Time。

1. <dependency> 
2. <groupId>joda-time</groupId> 
3. <artifactId>joda-time</artifactId> 
4. <version>2.10.4</version> 
5. </dependency> 

总结

上面主要是总结了,java 8中日期,时间常用操作及处于java 6,java7 中使用新API的解决方案。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表