前言
此篇博客借鉴与博主: 某科学的南条
内容中也有自己的思考和总结
众所周知,SimpleDateFormat在多线程环境下是不安全的, 因为其内部使用的是一个calendar对象, 那么在多线程环境下使用同一个对象去修改不同的数据必然会造成不安全问题。
解决办法就是使用Java8提供LocalDate、LocalTime、LocalDateTime、DateTimeFormatter,这些都是线程安全的, 而且内置了很多优秀而又方便的方法
下面说说他们具体代表什么
LocalDate:只含年月日的日期对象
LocalTime:只含时分秒的时间对象
LocalDateTime:同时含有年月日时分秒的日期对象
DateTimeFormatter:用来转换时间格式的对象
获取日期API
public static void main(String[] args) {
//1、获取当前日期
LocalDate now = LocalDate.now();
System.out.println("当前时间 = " + now);
//2、获取指定日期(参数依次 年、月、日)
LocalDate localDate = LocalDate.of(2021, 3, 4);
System.out.println("年月日 = " + localDate);
//3、获取当前时间
LocalTime localTime = LocalTime.now();
System.out.println("localTime = " + localTime);
//4、获取指定时间(参数依次 时、分、秒、纳秒
LocalTime localTimeOf = LocalTime.of(17, 24, 12, 4444);
System.out.println("localTimeOf = " + localTimeOf);
//5、获取当前年月日,时分秒都有的日期
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);
//6、获取指定年月日,时分秒都有的日期(参数依次 年、月、日、时、分)
LocalDateTime localDateTimeOf = LocalDateTime.of(2021, 3, 4, 17, 12);
System.out.println("localDateTimeOf = " + localDateTimeOf);
//7、日期+时间 组成 包含年月日,时分秒都有的日期
LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now());
System.out.println("of = " + of);
}
输出结果
当前时间 = 2021-03-04
年月日 = 2021-03-04
localTime = 17:22:46.456
localTimeOf = 17:24:12.000004444
localDateTime = 2021-03-04T17:22:46.456
localDateTimeOf = 2021-03-04T17:12
of = 2021-03-04T17:22:46.457
Process finished with exit code 0
日期的计算
对于LocalDate,只有精度大于或等于日的加减,如年、月、日;
对于LocalTime,只有精度小于或等于时的加减,如时、分、秒、纳秒;
对于LocalDateTime,则可以进行任意精度的时间相加减;
加法操作
public static void main(String[] args) {
//获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前时间 = " + localDateTime);
//1、加1年
LocalDateTime plusYears = localDateTime.plusYears(1L);
System.out.println("plusYears = " + plusYears);
//2、加1个月
LocalDateTime plusMonths = localDateTime.plusMonths(1L);
System.out.println("plusMonths = " + plusMonths);
//3、加一天
LocalDateTime plusDays = localDateTime.plusDays(1L);
System.out.println("plusDays = " + plusDays);
//4、加1个小时
LocalDateTime plusHours = localDateTime.plusHours(1L);
System.out.println("plusHours = " + plusHours);
//5、加10分
LocalDateTime plusMinutes = localDateTime.plusMinutes(10L);
System.out.println("plusMinutes = " + plusMinutes);
//6、加200毫秒
LocalDateTime plusSeconds = localDateTime.plusSeconds(200L);
System.out.println("plusSeconds = " + plusSeconds);
//7、加一小时
LocalDateTime plusHours2 = localDateTime.plus(1, ChronoUnit.DAYS);
//8、加一周
LocalDateTime plusWeeks = localDateTime.plus(1, ChronoUnit.WEEKS);
//9、加一年
LocalDateTime plusYears2 = localDateTime.plus(1, ChronoUnit.YEARS);
}
输出结果
当前时间 = 2021-03-04T17:29:35.153
plusYears = 2022-03-04T17:29:35.153
plusMonths = 2021-04-04T17:29:35.153
plusDays = 2021-03-05T17:29:35.153
plusHours = 2021-03-04T18:29:35.153
plusMinutes = 2021-03-04T17:39:35.153
plusSeconds = 2021-03-04T17:32:55.153
Process finished with exit code 0
减法操作
public static void main(String[] args) {
//获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前时间 = " + localDateTime);
//1、减1年
LocalDateTime plusYears = localDateTime.minusYears(1L);
System.out.println("plusYears = " + plusYears);
//2、减1个月
LocalDateTime plusMonths = localDateTime.minusMonths(1L);
System.out.println("plusMonths = " + plusMonths);
//3、减一天
LocalDateTime plusDays = localDateTime.minusDays(1L);
System.out.println("plusDays = " + plusDays);
//4、减1个小时
LocalDateTime plusHours = localDateTime.minusHours(1L);
System.out.println("plusHours = " + plusHours);
//5、减10分
LocalDateTime plusMinutes = localDateTime.minusMinutes(10L);
System.out.println("plusMinutes = " + plusMinutes);
//6、减200毫秒
LocalDateTime plusSeconds = localDateTime.minusSeconds(200L);
System.out.println("plusSeconds = " + plusSeconds);
//7、减一小时
LocalDateTime plusHours2 = localDateTime.minus(1, ChronoUnit.DAYS);
//7、减一周
LocalDateTime plusWeeks = localDateTime.minus(1, ChronoUnit.WEEKS);
//7、减一年
LocalDateTime plusYears2 = localDateTime.minus(1, ChronoUnit.YEARS);
}
从代码中可以看到,记忆起来也是很方便,这些 plus() 和 minus() 方法,是不会改变原date和time的实例的,返回的是新的实例。
比较日期和时间
当我们想知道给定的时间或日期是在另一个时间/日期之前还是之后,我们就可以用到isBefore()、isAfter()和compareTo方法。
对于compareTo方法, 如果前一个参数表示的时间是大于后一个的就返回一个大于0的数字, 如果小于就返回一个小于0的数据, 使用这个方法要注意,LocalDate只能比较LocalDate, LocalDateTime只能比较LocalDateTime,不然那就会报错!
public static void main(String[] args) {
LocalDate ld1 = LocalDate.of(2021, 3, 4);
LocalDate ld2 = LocalDate.of(2020, 4, 3);
boolean after = ld1.isAfter(ld2);
System.out.println("ld1是否在ld2之后 = " + after);
//输出: ld1是否在ld2之后 = false
boolean before = ld1.isBefore(ld2);
System.out.println("ld1是否在ld2之前 = " + before);
//输出: ld1是否在ld2之前 = true
LocalDateTime ldt1 = LocalDateTime.of(2020, 3, 4, 18, 12);
LocalDateTime ldt2 = LocalDateTime.of(2020, 3, 4, 18, 12);
boolean after1 = ldt1.isAfter(ldt2);
System.out.println("ldt1是否在ldt2之后 = " + after1);
//输出: ldt1是否在ldt2之后 = false
boolean before1 = ldt1.isBefore(ldt2);
System.out.println("ldt1是否在ldt2之后 = " + before1);
//输出: ldt1是否在ldt2之后 = true
//时间相减
Duration duration = Duration.between(ldt1, ldt2);
//两个时间差的天数
long days = duration.toDays();
System.out.println("days = " + days);
//输出: days = 0
//小时数差
long hours = duration.toHours();
System.out.println("hours = " + hours);
//输出: hours = 2
//分钟数差
long minutes = duration.toMinutes();
System.out.println("minutes = " + minutes);
//输出: minutes = 120
//毫秒数差
long millis = duration.toMillis();
System.out.println("millis = " + millis);
//输出: millis = 7200000
//纳秒数差
long nanos = duration.toNanos();
System.out.println("nanos = " + nanos);
//输出: nanos = 7200000000000
//时间比较
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.compareTo(ChronoLocalDateTime.from(localDate)));
}
ISO标准日期格式转换
如果你遵循ISO标准在日期/时间和字符串之间进行转换,那么这个事情会变得很容易,因为在 DateTimeFormatter 中,已经内置了ISO标准的格式。
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println("ldt = " + ldt);
//输出: ldt = 2020-07-07T18:32:34.757
String format1 = ldt.format(DateTimeFormatter.ISO_DATE);
System.out.println("format1 = " + format1);
//输出: format1 = 2020-07-07
String format2 = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("format2 = " + format2);
//输出: format2 = 20200707
String format3 = ldt.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println("format3 = " + format3);
//输出: format3 = 2020-07-07T18:32:34.757
String format4 = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println("format4 = " + format4);
//输出: format4 = 2020-07-07T18:32:34.757
String format = ldt.format(DateTimeFormatter.ofPattern("d-M-y"));
System.out.println("format = " + format);
//输出: format = 7-7-2020
String format5 = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("format5 = " + format5);
//输出: format5 = 2020-07-07 18:32:34
String format6 = ldt.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒"));
System.out.println("format6 = " + format6);
//输出: format6 = 2020年07月07日18时32分34秒
}
有木有感觉简直不要方便太多。
String转日期
如果使用字符串解析成时间DateTimeFormatter也是支持的, 你想要转换成什么时间类型, 直接使用LocalDataXXX.parse(str, DateTimeFormatter.ofPattern(yyyy-mm-dd));
public static void main(String[] args) {
LocalDate ld = LocalDate.parse("2020-07-07");
System.out.println("ld = " + ld);
//输出: ld = 2020-07-07
String str = "2020-07-07 22:24:33";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(str,dateTimeFormatter);
System.out.println("ldt = " + ldt);
//输出: ldt = 2020-07-07T22:24:33
}
其他好玩的API
public static void main(String[] args) {
LocalDate date = LocalDate.parse("2021-03-04");
//获取这个月的第一个周末的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY)));
//获取上个月的最后一周末的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY)));
//获取这个月的倒数第一个周末的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(-1, DayOfWeek.SUNDAY)));
//获取这个月的第一个周末的时间,上面的dayOfWeekInMonth更灵活,可以定义第几周
System.out.println(date.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));
//明年的第一天
System.out.println(date.with(TemporalAdjusters.firstDayOfNextYear()));
//获取下个周5的时间
System.out.println(date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
//获取本月最后一天
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth()));
//获取本月第一天
System.out.println(date.with(TemporalAdjusters.firstDayOfMonth()));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//一天开始时间
LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
String format = todayStart.format(dateTimeFormatter);
System.out.println("format = " + format);
//一天结束时间
LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
String format1 = todayEnd.format(dateTimeFormatter);
System.out.println("format1 = " + format1);
//一天中午时间
LocalDateTime todayMid = LocalDateTime.of(LocalDate.now(), LocalTime.NOON);
String format2 = todayMid.format(dateTimeFormatter);
System.out.println("format2 = " + format2);
}