JavaJava8 DateTime API
DreamCollector一、Java8时间处理
由于java.util.Date 设定为可变类型,以及 SimpleDateFormat 的非线程安全使其应用非常受限,优先推荐使用java.time 包下的所有类因为其实不可变类型而且线程安全的
类名称 |
描述 |
Instant |
时间戳 |
Duration |
持续时间,时间差 |
LocalData |
只包含日期,例如:2011-03-12 |
LocalTime |
只包含时间,例如:21:35:10 |
LocalDateTime |
包含时间和日期,例如:2011-03-12 21:35:10 |
Period |
时间段 |
ZoneOffset |
时间偏移量,比如:+8:00 |
ZoneDateTime |
带时区的时间 |
Clock |
时钟,比如获取目前美国纽约的时间 |
1. 获取当前日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Data { public static void main(String[] args) { System.out.println(LocalDate.now()); System.out.println(LocalTime.now()); Clock clock = Clock.systemUTC(); Clock defaultClock = Clock.systemDefaultZone(); System.out.println("Clock : " + clock.millis()); System.out.println("Clock : " + defaultClock.millis()); Instant timestamp = Instant.now(); System.out.println(timestamp.toEpochMilli()); } }
|
2. 获取年月日信息
1 2 3 4 5 6 7
| public class Data { public static void main(String[] args) { System.out.println(today.getYear()); System.out.println(today.getMonthValue()); System.out.println(today.getDayOfMonth()); } }
|
3. 返回自定义日期对象
1 2 3 4 5 6
| public class Data { public static void main(String[] args) { LocalDate customData = LocalDate.of(2021,10,9); System.out.println(customData); } }
|
4. 判断两日期是否相等
1 2 3 4 5 6 7
| public class Data { public static void main(String[] args) { LocalDate customData = LocalDate.of(2021,10,9); LocalDate today = LocalDate.now(); System.out.println(today.equals(customData)); } }
|
5. 判断像生日这种周期性事件(仅匹配同月同日不管年份)
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { LocalDate customData = LocalDate.of(2021,10,9); MonthDay birthday = MonthDay.of(customData.getMonth(),customData.getDayOfMonth()); MonthDay currentMonthDay = MonthDay.from(today); System.out.println(currentMonthDay.equals(birthday)); } }
|
6. 判断日期是早于还是晚于另一个日期
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate tomorrow = LocalDate.of(2018,2,6); System.out.println(tomorrow.isAfter(today)); System.out.println(tomorrow.isBefore(today)); } }
|
7. 判断平闰年
1 2 3 4 5 6
| public class Data { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println(today.isLeapYear()); } }
|
8. 获取两个日期之间相差的天数和月数
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate releaseDate = LocalDate.of(2018, 12, 14); Period monthsNum = Period.between(today, releaseDate); System.out.println(periodToNextJavaRelease.getMonths()); } }
|
9. 时间处理
1 2 3 4 5 6 7 8 9
| public class Data { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalTime newTime = LocalTime.now().plusHours(3); System.out.println(newTime); LocalTime newTime=LocalTime.now().minusHours(3); System.out.println(newTime); } }
|
10. 日期处理
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { LocalDate nextWeek = LocalDate.now().plus(1, ChronoUnit.WEEKS); System.out.println(newTime); LocalDate previousYear = LocalDate.now().minus(1, ChronoUnit.YEARS); System.out.println(previousYear); } }
|
11. 时区处理
1 2 3 4 5 6 7 8 9
| public class Data { public static void main(String[] args) { ZoneId america = ZoneId.of("America/New_York"); LocalDateTime localtDateAndTime = LocalDateTime.now(); ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america); System.out.println(dateAndTimeInNewYork); } }
|
12. 格式化字符串日期
1 2 3 4 5 6 7
| public class Data { public static void main(String[] args) { LocalDate formatDate = LocalDate.parse("2022-01-03",DateTimeFormatter.ISO_LOCAL_DATE); System.out.println(formatDate); } }
|
13. 日期转字符串
1 2 3 4 5 6 7
| public class Data { public static void main(String[] args) { LocalDateTime date = LocalDateTime.now(); String str = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")); System.out.println(str); } }
|
14. 字符串转日期
1 2 3 4 5 6 7 8 9 10 11
| public class Data { public static void main(String[] args) { String str="2022-08-03 15:02:16"; DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDate date1 = LocalDate.parse(str,DateTimeFormatter.ISO_LOCAL_DATE); LocalDate date2 = LocalDate.parse(str,format); System.out.println(date1); System.out.println(date2); } }
|
15. 日期转时间戳
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { LocalDateTime today = LocalDateTime.now(); Long timestamp=today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
} }
|
16. 时间戳转日期
1 2 3 4 5 6 7 8
| public class Data { public static void main(String[] args) { long milli=1663830008227L; LocalDate newDate = Instant.ofEpochMilli(milli).atZone(ZoneId.systemDefault()).toLocalDate();
} }
|