
在使用Date ,Calendar ,GregoiranCalendar ,SimpleDateFormat 来分别处理日期、日历、公历、和日期时间格式化时可能产生一些问题,因为它们四个都是线程不安全的(而且处理很复杂)。
在项目开发中,已经需要对Date类型进行格式处理,否则可读性很差,格式化Date类型要使用SimpleDateFormat,但SimpleDateFormat是线程不安全的。而使用这JDK8新特性里提供的3个时间类:LocalDate、LocalTime、LocalDateTime可以大大简化我们的处理过程。
带着问题看文章,看完这篇文章后是不是更容易解决呢?
1:计算明天是星期几?它是这个月的第几天?是今年的第几天?
2:下个月是几月?如果当前为12月呢?
2.关于LocalDate、LocalTime、LocalDateTime 2.1 LocalDate (会获取年月日)3:20天后是什么时间(精确到年月日)?20天前呢(精确到年月日)?
//获取当前年月日 LocalDate localDate = LocalDate.now(); //构造指定的年月日 LocalDate localDate1 = LocalDate.of(2019, 9, 10); //年 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);2.2 LocalTime (会获取时分秒)
//创建LocalTime LocalTime localTime = LocalTime.of(13, 51, 10); LocalTime localTime1 = LocalTime.now(); //获取小时 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);2.3 LocalDateTime(年月日时分秒)
//获取年月日时分秒,等于LocalDate+LocalTime //创建LocalDateTime LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56); LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime); LocalDateTime localDateTime3 = localDate.atTime(localTime); LocalDateTime localDateTime4 = localTime.atDate(localDate); //获取LocalDate LocalDate localDate2 = localDateTime.toLocalDate(); //获取LocalTime LocalTime localTime2 = localDateTime.toLocalTime();2.4 Instant(秒,毫秒)
//创建Instant对象 Instant instant = Instant.now(); //获取秒数 long currentSecond = instant.getEpochSecond(); //获取毫秒数 long currentMilli = instant.toEpochMilli();3.增减年月日时分秒的方法(plus/minus系列的方法) 3.1 增加相关的方法
plusYears(int offset)//:增加指定年份
plusMonths(int offset)//:增加指定月份
plusWeeks(int offset)//:增加指定周
plusDates(int offset)//:增加指定日
plusHours(int offset)//:增加指定时
plusMinuets(int offset)//:增加指定分
plusSeconds(int offset)//:增加指定秒
plusNanos(int offset)//:增加指定纳秒
3.2 减少相关的方法
minusYears(int offset)//:减少指定年
minusMonths(int offset)//:减少指定月
minusWeeks(int offset)//:减少指定周
minusDates(int offset)//:减少指定日
minusHours(int offset)//:减少指定时
minusMinuets(int offset)//:减少指定分
minusSeconds(int offset)//:减少指定秒
minusNanos(int offset)//:减少指定纳秒
3.3代码演示:年月日增加删除
public class TimeOfStudy {
public static void main(String[] args) {
//当前时间为2022-02-14
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
//年
int year = localDate.getYear();
int year02 = localDate.plusYears(1L).getYear();
int year03 = localDate.minusYears(1L).getYear();
//打印信息
System.out.println(year + "--->year");
System.out.println(year02 + "--->year2");
System.out.println(year03 + "--->year2");
//月
Month month = localDate.getMonth();
int monthValue = localDate.getMonthValue();
int monthValue02 = localDate.plusMonths(1L).getMonthValue();
int monthValue03 = localDate.minusMonths(1L).getMonthValue();
//打印信息
System.out.println(month + "--->");
System.out.println(monthValue + "--->monthValue");
System.out.println(monthValue02 + "--->monthValue02");
System.out.println(monthValue03 + "--->monthValue03");
//日
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfMonth = localDate.getDayOfMonth();
int dayOfMonth02 = localDate.plusDays(1L).getDayOfMonth();
int dayOfMonth03 = localDate.minusDays(1L).getDayOfMonth();
int dayOfYear = localDate.getDayOfYear();
//打印信息
System.out.println(dayOfWeek + "--->dayOfWeek");
System.out.println(dayOfMonth + "--->dayOfMonth");
System.out.println(dayOfMonth02 + "--->dayOfMonth02");
System.out.println(dayOfMonth03 + "--->dayOfMonth03");
System.out.println(dayOfYear + "--->dayOfYear");
}
}
4.与Data的相互转化
看源码:https://www.matools.com/api/java8
//创建一个时间 Date date = new Date(); //获取系统时区 ZoneId zoneId = ZoneId.systemDefault(); //使用ofInstant转化 LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);