本文主要是介绍LocalDateTime用法大全,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
常规使用
LocalDateTime now = LocalDateTime.now(); // 2020-01-29T14:35:51.207
int year = now.getYear(); // 2020
Month month = now.getMonth(); // JANUARY
int monthValue = now.getMonthValue(); // 1
int dayOfYear = now.getDayOfYear(); // 29
int dayOfMonth = now.getDayOfMonth(); // 29
DayOfWeek dayOfWeek = now.getDayOfWeek(); // WEDNESDAY
int dayOfWeekValue = dayOfWeek.getValue(); // 3
int hour = now.getHour(); // 14
int minute = now.getMinute(); // 35
int second = now.getSecond(); // 51long seconds = Instant.now().getEpochSecond(); // 秒时间戳(10位)
long milliSeconds = Instant.now().toEpochMilli(); // 毫秒时间戳(13位)LocalDateTime dateTimeFromSecond = LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneOffset.ofHours(8)
); // 秒转datetimeLocalDateTime dateTimeFromMilliSecond = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds), ZoneOffset.ofHours(8)
); // 毫秒转datetime
各种转换
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
); // datetime转字符串LocalDateTime dateTime = LocalDateTime.parse(format,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
); // 字符串转datetimeDate date = Date.from(dateTime.toInstant(ZoneOffset.ofHours(8))); // datetime转date
LocalDateTime fromDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime(); // date转datetime
自定义日期 / 日期加减
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 29, 14, 35, 51); // 2020-01-29T14:35:51LocalDateTime localDateTime1 = dateTime.minusSeconds(1); // 2020-01-29T14:35:50
LocalDateTime localDateTime2 = dateTime.minusMinutes(1); // 2020-01-29T14:34:51
LocalDateTime localDateTime3 = dateTime.minusHours(1); // 2020-01-29T13:35:51
LocalDateTime localDateTime4 = dateTime.minusDays(1); // 2020-01-28T14:35:51
LocalDateTime localDateTime5 = dateTime.minusWeeks(1); // 2020-01-22T14:35:51
LocalDateTime localDateTime6 = dateTime.minusMonths(1); // 2019-12-29T14:35:51
LocalDateTime localDateTime7 = dateTime.minusYears(1); // 2019-01-29T14:35:51
增加的为对应的plus函数,如
dateTime.plusSeconds(1)
比较日期
LocalDateTime now = LocalDateTime.now(); // 2020-01-29T14:35:51.207
LocalDateTime yesterday = now.minusDays(1); // 2020-01-28T14:35:51.207
boolean after = now.isAfter(yesterday); // true
boolean before = now.isBefore(yesterday); // false
boolean equal = now.isEqual(yesterday); // false
其他类
LocalDate日期
LocalDate dateNow = LocalDate.now(); // 2020-01-29
LocalDate date = LocalDate.of(2020, 1, 29); // 2020-01-29
LocalTime时间
LocalTime timeNow = LocalTime.now(); // 14:35:51.207
LocalTime time = LocalTime.of(23, 59, 59); // 23:59:59
LocalDateTime日期时间
LocalDateTime dateTimeNow = LocalDateTime.now(); // 2020-01-29T14:35:51.207
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 29, 23, 59, 59); // 2020-01-29T23:59:59
Instant时刻
long second = Instant.now().getEpochSecond(); // 1580279751
long milliSecond = Instant.now().toEpochMilli(); // 1580279751000
Instant instant1 = Instant.ofEpochSecond(second); // 2020-01-29T14:35:51Z
Instant instant2 = Instant.ofEpochMilli(milliSecond); // 2020-01-29T14:35:51.207Z
Duration时间差
Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusDays(1)
); // PT24H
long seconds = duration.getSeconds(); // 86400
long seconds2 = duration.get(ChronoUnit.SECONDS); // 86400Duration ofDays = Duration.ofDays(1); // PT24H
Duration ofHours = Duration.ofHours(1); // PT1H
Duration ofMinutes = Duration.ofMinutes(1); // PT1M
Duration ofSeconds = Duration.ofSeconds(1); // PT1S
Period时间段
Period between = Period.between(LocalDate.of(2020, 1, 22), LocalDate.of(2020, 2, 9)
); // P18D
int year = between.getYears(); // 0
int month = between.getMonths(); // 0
int day = between.getDays(); // 18Period period = Period.of(1, 2, 3); // P1Y2M3D
long years = period.get(ChronoUnit.YEARS); // 1
long months = period.get(ChronoUnit.MONTHS); // 2
long days = period.get(ChronoUnit.DAYS); // 3
Clock时钟
Clock clock1 = Clock.system(ZoneOffset.ofHours(8)); // SystemClock[+08:00]
Clock clock2 = Clock.systemUTC(); // SystemClock[Z]long millis1 = clock1.millis(); // 1582285924008
long millis2 = clock2.millis(); // 1582285924008
Instant instant1 = clock1.instant(); // 2020-02-21T11:54:58.427Z
Instant instant2 = clock2.instant(); // 2020-02-21T11:54:58.428Z
天猫超市优惠地址
这篇关于LocalDateTime用法大全的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!