LocalDate
只会获取年月日
- 创建LocalDate
// 获取当前年月日
LocalDate localDate = LocalDate.now();
// 构造指定的年月日
LocalDate localDate1 = LocalDate.of(2019, 11, 1);
- 获取年、月、日、星期
// 年
int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
// 月
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
// 日
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
// 星期
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
LocalTime
只会获取时分秒
- 创建LocalTime
// 当前时分秒
LocalTime localTimeNow = LocalTime.now();
// 指定时分秒
LocalTime localTime = LocalTime.of(13, 51, 10);
- 获取时、分、秒
// 获取时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
// 获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
// 获取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
LocalDateTime
获取年月日时分秒,等价于LocalDate + LocalTime
- 获取LocalDateTime
// 当前
LocalDateTime localDateTime = LocalDateTime.now();
// 指定
LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
// 通过LocalDate和LocalTime获取
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
LocalDateTime localDateTime4 = localTime.atDate(localDate);
- 获取LocalDate
LocalDate localDate = localDateTime.toLocalDate();
- 获取LocalTime
LocalTime localTime = localDateTime.toLocalTime();
Instant
获取秒数
- 创建Instant对象
Instant instant = Instant.now();
- 获取秒数
long currentSecond = instant.getEpochSecond();
- 获取毫秒数
long currentMilli = instant.toEpochMilli();
等价于
System.currentTimeMillis()
修改LocalDate,LocalTime,LocalDateTime,Instant
LocalDate、LocalTime、LocalDateTime、Instant为不可变对象,修改这些对象对象会返回一个副本
- 增加、减少年数、月数、天数等,以LocalDateTime为例
LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10,
14, 46, 56);
// 增加一年
localDateTime = localDateTime.plusYears(1);
localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
// 减少一个月
localDateTime = localDateTime.minusMonths(1);
localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);
- 通过with修改某些值
可以修改年月日
// 修改年为2019
localDateTime = localDateTime.withYear(2019);
// 修改为2022
localDateTime = localDateTime.with(ChronoField.YEAR, 2022);
时间计算
比如有些时候想知道这个月的最后一天是几号、下个周末是几号,通过提供的时间和日期API可以很快得到答案。
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = localDate.with(firstDayOfYear());
格式化时间
LocalDate localDate = LocalDate.of(2019, 9, 10);
String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
// 自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String s3 = localDate.format(dateTimeFormatter);
DateTimeFormatter默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter的ofPattern方法创建自定义格式化方式
解析时间
LocalDate localDate1 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate2 = LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE);
和SimpleDateFormat相比,DateTimeFormatter是线程安全的
网友评论