LocalDate,LocalTime,LocalDateTime的基本使用
Java8提供的新的时间日期的类型LocalDate,LocalTime,LocalDateTime。之前用的Date和Calendar都为线程不安全。用来格式化Date类型的SimpleDateFormat也是线程不安全的。LocalDate和格式化LocalDate的DateTimeFormatter是线程安全的。
LocalDate的使用
创建LocalDate对象有两种方式:
// 创建当前日期的LocalDate
LocalDate localDate = LocalDate.now();
// 创建指定日期的LocalDate
LocalDate localDate1 = LocalDate.of(2020,4,14);
从LocalDate类型的对象中获取年,月,日,星期几
获取年月日周几都是有两种方式,一个getXxx()
,一种get(ChronoField.XXX)
// 获取年
int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
System.out.println(year + " -- " + year1); // 2020 -- 2020
// 获取月
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println("month = " + month); // month = APRIL
System.out.println("month.getValue() = " + month.getValue()); // month.getValue() = 4
System.out.println("month1 = " + month1); // month1 = 4
// Month类型也可以做加减法,month.plus(1),会返回一个Month对象,值为MAY
// 获取日,DayOfMonth
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println("day-day1 = " + day + " - " + day1);// day-day1 = 16 - 16
// 获取周几,DayOfWeek
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println("dayOfWeek = " + dayOfWeek); // THURSDAY
System.out.println("dayOfWeek.getValue()="+dayOfWeek.getValue());//dayOfWeek.getValue()=4
System.out.println("dayOfWeek1 = " + dayOfWeek1);// dayOfWeek1 = 4
// DayOfWeek也可以做加减法,具体方法.就能看到
LocalTime的使用
LocalTime localTime = LocalTime.now();
LocalTime localTime1 = 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的使用
// LocalDateTime就是LocalDate+LocalTime
// 创建LocalDateTime的几种方式
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2020, Month.APRIL, 4, 14, 46, 56);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
LocalDateTime localDateTime4 = localTime.atDate(localDate);
// 从LocalDateTime获取LocalDate和LocalTime
LocalDate localDate4 = localDateTime.toLocalDate();
LocalTime localTime2 = localDateTime.toLocalTime();
Instant的使用
// 创建Instant对象
Instant instant = Instant.now();
// 获取秒数
long epochSecond = instant.getEpochSecond();
// 获取毫秒 也可以用System.currentTimeMillis();
long currentMilli = instant.toEpochMilli();
格式化LocalDate和解析字符串到LocalDate
// 格式化时间 DateTimeFormatter是线程安全的,SimpleDateFormat是线程不安全的
// 使用DateTimeFormatter提供的默认的模板
String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); // yyyyMMdd
String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); // yyyy-MM-dd
//自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String s3 = localDate.format(dateTimeFormatter);
// 解析时间
LocalDate localDate5 = LocalDate.parse("20200414", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate6 = LocalDate.parse("2020-04-14", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate的加减法
LocalDate的加减法,有加减天数,月,周,年
对应的方法是plusDays(long),plusMonths(long),plusYears(long),plusWeeks(long),plus(1, ChronoUnit.YEARS)
,
minusDays(long),minusMonths(long),minusYears(long),minusWeeks(long),minus(1, ChronoUnit.MONTHS)
。
具体的方法和方法签名,直接.也能看到。
// 今天是2020-04-16
LocalDate localDate2 = localDate.plusDays(10);
System.out.println("localDate2.toString() = " + localDate2.toString());
LocalDate localDate3 = localDate.minusMonths(1);
System.out.println("localDate3.toString() = " + localDate3.toString());
通过with修改某些值
只修改年,不修改月日
LocalDate localDate = LocalDate.now();
//修改年为2021
localDate = localDate.withYear(2021);
//修改为2022
localDate = localDate.with(ChronoField.YEAR, 2022);
有些时候想知道这个月的最后一天是几号、下个周末是几号,通过提供的时间和日期API可以很快得到答案。
使用TemporalAdjusters中的一些提供的方法:
LocalDate localDate7 = LocalDate.now();
localDate7 = localDate7.with(TemporalAdjusters.firstDayOfYear());
跟着LocalDate中with方法往下读的一些源码
在LocalDate的源码中,with有这么一些方法:
// with方法,需要一个 TemporalAdjuster类型的对象,
public LocalDate with(TemporalAdjuster adjuster) {
/*
LocalDate实现了TemporalAdjuster接口,这里也能直接传入LocalDate的对象。看这个方法中的逻辑可以明白,如果adjuster是LocalDate的实例,那么强转为LocalDate类型。
如果不传入LocalDate类型的,只需要传入实现了TemporalAdjuster接口的对象,会调用TemporalAdjuster中的adjustInto方法。可以看到TemporalAdjuster的adjuster方法返回的是Temporal类型的对象,强转为LocalDate类型。
*/
return adjuster instanceof LocalDate ? (LocalDate)adjuster : (LocalDate)adjuster.adjustInto(this);
}
public LocalDate with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField)field;
f.checkValidValue(newValue);
switch(f) {
case DAY_OF_MONTH:
return this.withDayOfMonth((int)newValue);
case DAY_OF_YEAR:
return this.withDayOfYear((int)newValue);
// 这里还有很多case语句,省略...
} else {
return (LocalDate)field.adjustInto(this, newValue);
}
}
// 下面这些都是替换具体的int的值
public LocalDate withYear(int year){//...
}
public LocalDate withMonth(int month){//...
}
public LocalDate withDayOfMonth(int dayOfMonth){//...
}
public LocalDate withDayOfYear(int dayOfYear){//...
}
在TemporalAdjusters这个类中,有许多的方法,都是返回一个实现好的TemporalAdjuster的引用类型
public static TemporalAdjuster firstDayOfYear() {
return (temporal) -> {
return temporal.with(ChronoField.DAY_OF_YEAR, 1L);
};
}
public static TemporalAdjuster lastDayOfYear() {
return (temporal) -> {
return temporal.with(ChronoField.DAY_OF_YEAR, temporal.range(ChronoField.DAY_OF_YEAR).getMaximum());
};
}
参考: