LocalDateTime对日期的一些获取操作:
LocalDate内部维护了LocalDate和LocalTime(内置方法基本相通)
大可使用LocalDateTime的相关方法获取时间的一些相关数值
LocalDateTime now = LocalDateTime.now(); // 2021-05-03T16:02:46.498
int year = now.getYear(); // 2021
Month month = now.getMonth(); // MAY
int monthValue = now.getMonthValue(); // 5
int dayOfYear = now.getDayOfYear(); // 123
int dayOfMonth = now.getDayOfMonth(); // 3
DayOfWeek dayOfWeek = now.getDayOfWeek(); // MONDAY
int dayOfWeekValue = dayOfWeek.getValue(); // 1
int hour = now.getHour(); // 16
int minute = now.getMinute(); // 2
int second = now.getSecond(); // 46
long seconds = Instant.now().getEpochSecond(); // 秒时间戳(10位) 1620028966
long milliSeconds = Instant.now().toEpochMilli(); // 毫秒时间戳(13位) 1620028966499
LocalDateTime dateTimeFromSecond = LocalDateTime.ofInstant(
Instant.ofEpochSecond(seconds), ZoneOffset.ofHours(8)
); // 秒转datetime 2021-05-03T16:02:46
LocalDateTime dateTimeFromMilliSecond = LocalDateTime.ofInstant(
Instant.ofEpochMilli(milliSeconds), ZoneOffset.ofHours(8)
); // 毫秒转datetime 2021-05-03T16:02:46.499
日期格式化转换&与Date之间转化
格式化日期使用DateTimeFormatter.
LocalDateTime和Date都有toInstant()方法
//localDateTime转字符串
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));// 2021-05-03 17:08:02
//字符串转localDateTime
LocalDateTime dateTime = LocalDateTime.parse(format,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));// 2021-05-03T17:08:02
//localDateTime转Date
Date date = Date.from(dateTime.toInstant(ZoneOffset.ofHours(8)));// Mon May 03 17:08:02 CST 2021
//Date转localDateTime
LocalDateTime fromDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();// 2021-05-03T17:08:02
日期加减
/*plus->加 minus->减*/
LocalDateTime localDateTime = LocalDateTime.now();
//以减法为例:plus对应+
LocalDateTime localDateTime1 = localDateTime.minusSeconds(1);
LocalDateTime localDateTime2 = localDateTime.minusMinutes(1);
LocalDateTime localDateTime3 = localDateTime.minusHours(1);
LocalDateTime localDateTime4 = localDateTime.minusDays(1);
LocalDateTime localDateTime5 = localDateTime.minusWeeks(1);
LocalDateTime localDateTime6 = localDateTime.minusMonths(1);
LocalDateTime localDateTime7 = localDateTime.minusYears(1);
判断日期大小
LocalDateTime now = LocalDateTime.now();
LocalDateTime yesterday = now.minusDays(1);
boolean after = now.isAfter(yesterday);// 'now'是'昨天'之后?true
boolean before = now.isBefore(yesterday);// 'now'是'昨天'之前?false
boolean equal = now.isEqual(yesterday);// 相等?false
其他类
Instant:时刻
Instant instant=Instant.now();
long second = instant.getEpochSecond();
long milliSecond = instant.toEpochMilli();
Duration:时间差
Duration内部维护了seconds(秒)和nanos(纳秒)
(获取一个时间差值时使用)
Duration duration = Duration.between(
LocalDateTime.now(), LocalDateTime.now().plusDays(1)
); // PT24H
/*可以直接获取秒和纳秒(getXX),但是换算成天、小时、分钟等需要换算(toXX)*/
long seconds = duration.getSeconds(); // 86400
long seconds2 = duration.get(ChronoUnit.SECONDS); // 86400
int nano = duration.getNano(); // 0
long nanos = duration.get(ChronoUnit.NANOS); // 0
long days = duration.toDays(); //换算成天
long hours = duration.toHours(); //换算成小时...
//其他创建方法:
Duration 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內部维护了三个int(year,month,day)
(偏向人的理解,不能使用它计算差值,需组合使用)
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(); // 18
Period 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:时钟
有4个实现类,例子使用的是SystemClock;
永远返回最新的时间!!创建时需要指定时区。
Clock clock1 = Clock.system(ZoneOffset.ofHours(8)); // SystemClock[+08:00]
Clock clock2 = Clock.systemUTC(); // SystemClock[Z]
Clock clock3 = Clock.systemDefaultZone(); // SystemClock[Asia/Shanghai]
long millis1 = clock1.millis(); // 1620203451235
long millis2 = clock2.millis(); // 1620203451235
Thread.sleep(12312);
long millis3 = clock2.millis(); // 1620203463548
Instant instant1 = clock1.instant(); // 2021-05-05T08:31:03.548Z
Instant instant2 = clock2.instant(); // 2021-05-05T08:31:03.548Z
Thread.sleep(2342);
Instant instant3 = clock2.instant(); // 2021-05-05T08:31:05.891Z
分析一波Clock:
millis1、millis2、millis3可以看出,时区对获取时间戳没有影响,所以1.时间戳是不存在时区区别的,2.SystemClock永远返回最新的时间(Instant)
ChronoUnit:日期差值
该类适合用来计算日期时间的差值(重点使用)
long years = ChronoUnit.YEARS.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
long months = ChronoUnit.MONTHS.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
long days = ChronoUnit.DAYS.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
long hours = ChronoUnit.HOURS.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
long minutes = ChronoUnit.MINUTES.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
long seconds = ChronoUnit.SECONDS.between( LocalDateTime.of(2013,3,14,12,34,34),LocalDateTime.now());
log.info("相差:{}年",years);
log.info("相差:{}月",months);
log.info("相差:{}日",days);
log.info("相差:{}时",hours);
log.info("相差:{}分",minutes);
log.info("相差:{}秒",seconds);
/*
相差:9年
相差:110月
相差:3354日
相差:80498时
相差:4829890分
相差:289793422秒
*/
总结
Duration和Period相比,Duration中维护的是秒和纳秒,Period维护的是年月日,所以Duration时间粒度更小。
Clock和Instant相比,Instant内部维护了seconds和nanos,通过Clock每次获取Instant都是最新时间,Instant基本等同于维护了一个时间戳(seconds+nanos)仅仅适合作转化中间值,Clock内部维护的是时区。
Duration和Period获取时间差的时候,可以使用get方法,参数使用枚举:ChronoUnit定义时间粒度。或者直接getSeconds、getDays...
通过参考LocalDateTime.now()方法,获取系统默认的ZoneOffSet:
ZoneOffset.ofHours(8) //OR Clock defaultClock=Clock.systemDefaultZone(); ZoneOffset zoneOffset = defaultClock.getZone().getRules().getOffset(defaultClock.instant());
月、周对应value值
没有0.都是1开始,符合现实说法
月 | Month | Month.getValue |
---|---|---|
1月 | JANUARY | 1 |
2月 | FEBRUARY | 2 |
3月 | MARCH | 3 |
4月 | APRIL | 4 |
5月 | MAY | 5 |
6月 | JUNE | 6 |
7月 | JULY | 7 |
8月 | AUGUST | 8 |
9月 | SEPTEMBER | 9 |
10月 | OCTOBER | 10 |
11月 | NOVEMBER | 11 |
12月 | DECEMBER | 12 |
周 | DayOfWeek | DayOfWeek.getValue |
---|---|---|
周一 | MONDAY | 1 |
周二 | TUESDAY | 2 |
周三 | WEDNESDAY | 3 |
周四 | THURSDAY | 4 |
周五 | FRIDAY | 5 |
周六 | SATURDAY | 6 |
周日 | SUNDAY | 7 |