前言:由于之前想要用Java实现日历的打印以及找到相应的寻找休息日,所以在这方面寻找相应的资料并进行总结归纳
Java中1.8之前有date类和calendar类。其中date类到了1.8大部分的方法被弃用了说明这些方法都有很大的缺陷,而且date类如果不格式化可读性十分差,所以就需要格式化,而格式化使用simpledateformat来进行操作线程不安全。而calendar类是一个共享变量,而且没有做线程不安全,当多个线程同时使用simpledateformat调用format方法时,多个线程会同时调用calendar.setTime方法,而导致的后果时time值一直被修改从而返回的格式化的时间是错误的。而且simpledateformat方法中format和parse方法都是线程不安全的。
Java1.8之后出现了localdate,localdatetime,localtime这些类,而这些类使用了final来修饰,使得这些类是不可变的,一旦实例化,值就固定了,有点类似于String类,所以这些类都是线程安全的。
localdate是针对于年月日的,localtime是针对于时分秒,localdatetime是针对于年月日时分秒
Localdate创建方法
//获取当前时间
LocalDate localDate = LocalDate.now();
//输入年月日设置时间
LocalDate localDate = LocalDate.of(year,month,day);
获取年月日星期几
int year = localDate.getYear();
int year = localDate.get(ChronoField.YEAR);
//注意getMonth方法返回的是Month类,可通过getValue方法获得一个long类型的值,然后可以强转为int类型进行使用
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
//注意getDayOfWeek方法返回的是DayOfWeek类,可通过getValue方法获得一个long类型的值,然后可以强转为int类型进行使用
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
进行加减日期
LocalDate localDate = LocalDate.now();
//增加一年
localDate = localDate.plusYears(1);
localDate = localDate.plus(1, ChronoUnit.YEARS);
//减少一个月
localDate = localDate.minusMonths(1);
localDate = localDate.minus(1, ChronoUnit.MONTHS);
//减少一日
localDate = localDate.minusDays(1);
localDate = localDate.minus(1, ChronoUnit.DAYS);
//通过使用with方法进行修改
//修改年为2020
localDate = localDate.withYear(2020);
//修改为2020
localDate = localDate.with(ChronoField.YEAR, 2020);
LocalTime相类似就是把年月日换成时分秒就可以了
创建LocalDateTime可以使用LocalDateTime.of(locaDate,localTime),LocalDate.atTime(localTime),LocalTime.atDate(localDate)来进行创建
LocalDateTime获取LocalDate和LocalTime的方法如下
//获取LocalDate
LocalDate localDate = localDateTime.toLocalDate();
//获取LocalTime
LocalTime localTime = localDateTime.toLocalTime();
Instant
Instant类是1.8之后新增的,与1.8之前的Date类有点相似
Instant的创建方法
Instant instant = Instant.now();
//l为1970年1月1日至所记录时间的的毫秒数
Instant instant1 = Instant.ofEpochMilli(l);
设置时区偏移量
// 按时区设置偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));// 设置偏移量为8
获取秒数
long currentSecond = instant.getEpochSecond();
获取毫秒数
获取1970年1月1日至所记录时间的的毫秒数,类似于Date类中的getTime方法。
//获取毫秒数
long l = instant.toEpochMilli();
对于LocalDate,LocalTime,LocalDateTime,Instant类进行格式化需要使用DateTimeFormatter类
//String转换成日期类型
//String类型转换成LocalDate类型
LocalDate localDate = LocalDate.parse("2019-12-07");
//String类型转换为LocalTime类型
LocalTime localTime = LocalTime.parse("17:26:00");
//String类型转换为LocalDateTime类型
//按照12小时计算
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//按照24小时计算
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate localDate = LocalDate.parse("2019-12-07 07:43:53",formatter);
//日期类型转换成String类型
//localDate转换成String
LocalDate localDate = LocalDate.now();
//按照yyyyMMdd样式进行更改
String format = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
//按照yyyy-MM-dd样式进行更改
String format = localDate.format(DateTimeFormatter.ISO_DATE);
//自定义样式进行更改
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format = localDate.format(pattern);
//LocalTime类型转换为String类型
//按照xx:xx:xx.xx样式进行转换
LocalTime localTime = LocalTime.now();
String format3 = localTime.format(DateTimeFormatter.ISO_TIME);
//按照自定义样式进行转换
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String format4 = localTime.format(formatter);
//LocalDateTime类型转换为String类型
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format5 = localDateTime.format(formatter2);