jdk1.8新增的三个日期类
- LocalDate
- LocalTime
- LocalDateTime
以及格式化类(线程安全)
private static DateTimeFormatter dateFormatter = DateTiemFormatter.ofPattern(“yyyy-M-d”);
package basics;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* @author Created by yinrg on 2022年3月25日, 0025 下午 5:04
* @fileSimpleName DateDemo2
* @fileName basics.DateDemo2
* @description JDK1.8新增的时间API
*/
public class DateDemo2 {
private static DateTimeFormatter dateTimeFormatter01 = DateTimeFormatter.ofPattern("yyyy-M-d");
private static DateTimeFormatter dateTimeFormatter02 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter dateTimeFormatter03 = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s");
public static void main(String[] args) {
/**
* parse() 使用特定格式化程序从文本字符串中获取LocalDate的实例。
* 使用格式化程序解析文本,返回日期。
*/
LocalDate ld01 = LocalDate.parse("2022-03-01");
LocalDate ld02 = LocalDate.parse("2022-03-9", dateTimeFormatter01);
LocalDate ld03 = LocalDate.parse("2022-03-01", dateTimeFormatter02);
System.out.println(ld01);
System.out.println(ld02);
System.out.println(ld03);
// 格式化操作
System.out.println(dateTimeFormatter01.format(ld01));
System.out.println(dateTimeFormatter01.format(ld02));
System.out.println(dateTimeFormatter02.format(ld03));
System.out.println(daysBetween(ld01, ld02));
System.out.println(weekSunday(ld01));
System.out.println(weekMonday(ld02));
System.out.println(monthEnd(ld02));
System.out.println(monthStart(ld02));
// LocalDateTime
System.out.println(LocalDateTime.parse("2022-3-21 11:11:11", dateTimeFormatter03)); // 2022-03-21T11:11:11
/**
* LocalDateTime与Date之间的互相转换
* new Date().getTime() 是Date转Long
* new Date(long time) 是long转Date
*/
long nowMillisecond = getMillisecond(LocalDateTime.now());
System.out.println(new Date(nowMillisecond));
}
/**
* LocalDateTime转Long
* @param dateTime
* @return
*/
public static long getMillisecond(LocalDateTime dateTime){
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* Long转LocalDateTime
* @param timestamp
* @return
*/
public static LocalDateTime parse(Long timestamp){
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant,zone);
}
/**
* 获取两个日期之间差值
*
* @param from
* @param to
* @return
*/
public static long daysBetween(LocalDate from, LocalDate to) {
return from.until(to, ChronoUnit.DAYS);
}
/**
* 获取所在周日是哪一天
*
* @param date
* @return
*/
public static LocalDate weekSunday(LocalDate date) {
return date.plusDays(7 - date.getDayOfWeek().getValue());
}
/**
* 获取所在周一是哪一天
*
* @param date
* @return
*/
public static LocalDate weekMonday(LocalDate date) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
return date.minusDays(dayOfWeek.getValue() - 1);
}
/**
* 获取所在月末是哪一天
*
* @param date
* @return
*/
public static LocalDate monthEnd(LocalDate date) {
return date.with(TemporalAdjusters.lastDayOfMonth());
}
/**
* 获取所在月初是哪一天
*
* @param date
* @return
*/
public static LocalDate monthStart(LocalDate date) {
return LocalDate.of(date.getYear(), date.getMonth(), 1);
}
}