java8 LocalDateTime
Posted 几多心中创伤 —《谁伴我闯荡》
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8 LocalDateTime相关的知识,希望对你有一定的参考价值。
在多并发情况下使用SimpleDateFormat需格外注意 :SimpleDateFormat除了format是线程不安全以外,parse方法也是线程不安全的。
LocalDate
只获取年月日
LocalDate localDate = LocalDate.now(); //localDate:2020-06-01
LocalDate localDate1 = LocalDate.of(2020, 01, 01); //localDate1:2020-01-01
获取年、月、日、星期几
int year = localDate.getYear(); //2020 int year1 = localDate.get(ChronoField.YEAR); //2020 Month month = localDate.getMonth(); //JUNE int month1 = localDate.get(ChronoField.MONTH_OF_YEAR); //6 int day = localDate.getDayOfMonth(); //1 int day1 = localDate.get(ChronoField.DAY_OF_MONTH); //1 DayOfWeek dayOfWeek = localDate.getDayOfWeek(); //MONDAY int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK); //1
LocalTime
只获取时分秒
LocalTime localTime = LocalTime.of(23, 11, 11); //23:11:11 LocalTime localTime1 = LocalTime.now(); //23:15:51.710
获取时分秒
int hour = localTime.getHour(); //23 int hour1 = localTime.get(ChronoField.HOUR_OF_DAY); //23 int minute = localTime.getMinute(); //11 int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR); //11 int second = localTime.getSecond(); //11 int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE); //11
LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now(); //2020-06-01T23:23:50.559
Instant
获取秒数、获取毫秒数
Instant instant = Instant.now(); //2020-06-01T15:27:18.608Z long currentSecond = instant.getEpochSecond(); //1591025238 long currentMilli = instant.toEpochMilli(); //1591025238608
System.currentTimeMillis(); //1591025406619
//2020
以上是关于java8 LocalDateTime的主要内容,如果未能解决你的问题,请参考以下文章