参考链接地址:https://mp.weixin.qq.com/s/0HpT1N7eqzVhK0urLUAKlg
package com.example.localdate;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class DateUtils {
public static void main(String[] args) {
//获取当前年月日
LocalDate localDate = getNow();
System.out.println(localDate.toString());
System.out.println(localDate.getYear());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek().getValue());
//获取指定年月日
LocalDate localDate1 = getLocalDate(2022, 7, 24);
System.out.println(localDate1.toString());
System.out.println(localDate1.getYear());
System.out.println(localDate1.getMonthValue());
System.out.println(localDate1.getDayOfMonth());
System.out.println(localDate1.getDayOfWeek().getValue());
//判断日期是否相同
System.out.println(equalsLocalDate(localDate, localDate1));
}
//获取当前年月日
public static LocalDate getNow(){
LocalDate now = LocalDate.now();
return now;
}
public static LocalDate getLocalDate(int year, int month, int day){
LocalDate localDate = LocalDate.of(year, month, day);
return localDate;
}
//判断年月日是否相同
public static boolean equalsLocalDate(LocalDate date1, LocalDate date2){
return date1.equals(date2);
}
//判断月日是否相同,可用于校验是否是生日
public static boolean equalsMonthDay(LocalDate date){
LocalDate now = LocalDate.now();
MonthDay monthDay = MonthDay.of(date.getMonth(), date.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(now);
return currentMonthDay.equals(monthDay);
}
//获取当前年月日时分秒
public static LocalTime getNowTime(){
LocalTime now = LocalTime.now();
return now;
}
public static LocalTime addHours(int hours){
return addHours(getNowTime(), hours);
}
public static LocalTime addHours(LocalTime localTime, int hours){
return localTime.plusHours(hours);
}
public static LocalTime addMinutes(int minutes){
return addMinutes(getNowTime(), minutes);
}
public static LocalTime addMinutes(LocalTime localTime, int minutes){
return localTime.plusMinutes(minutes);
}
public static LocalTime addSeconds(int seconds){
return addSeconds(getNowTime(), seconds);
}
public static LocalTime addSeconds(LocalTime localTime, int seconds){
return localTime.plusSeconds(seconds);
}
public static LocalDate addWeek(int week){
return addWeek(getNow(), week);
}
public static LocalDate addWeek(LocalDate localDate, int week){
return localDate.plus(week, ChronoUnit.WEEKS);
}
public static LocalDate addYear(int year){
return addYear(getNow(), year);
}
public static LocalDate addYear(LocalDate localDate, int year){
return localDate.plus(year, ChronoUnit.YEARS);
}
public static LocalTime subHours(int hours){
return addHours(getNowTime(), hours);
}
public static LocalTime subHours(LocalTime localTime, int hours){
return localTime.minusHours(hours);
}
public static LocalTime subMinutes(int minutes){
return subMinutes(getNowTime(), minutes);
}
public static LocalTime subMinutes(LocalTime localTime, int minutes){
return localTime.minusMinutes(minutes);
}
public static LocalTime subSeconds(int seconds){
return subSeconds(getNowTime(), seconds);
}
public static LocalTime subSeconds(LocalTime localTime, int seconds){
return localTime.minusSeconds(seconds);
}
public static LocalDate subWeek(int week){
return subWeek(getNow(), week);
}
public static LocalDate subWeek(LocalDate localDate, int week){
return localDate.minus(week, ChronoUnit.WEEKS);
}
//减n年
public static LocalDate subYear(int year){
return subYear(getNow(), year);
}
//减n年
public static LocalDate subYear(LocalDate localDate, int year){
return localDate.minus(year, ChronoUnit.YEARS);
}
//获取时间戳
public static long getNowTimeStamp() {
// 1、
// Returns the current time based on your system clock and set to UTC.
// Clock clock = Clock.systemUTC();
// System.out.println("Clock : " + clock.millis());
// 2、
// Returns time based on system clock zone
// Clock defaultClock = Clock.systemDefaultZone();
// defaultClock.millis();
// 3、
// 你也可以使用Date类和Instant类各自的转换方法互相转换,
// 例如:Date.from(Instant) 将Instant转换成java.util.Date,
// Date.toInstant()则是将Date类转换成Instant类。
Instant timestamp = Instant.now();
long l = timestamp.toEpochMilli();
return l;
}
//上海时区的时间
public static ZonedDateTime getLocalDateTimeByShanghai() {
// Date and time with timezone in Java 8
ZoneId america = ZoneId.of("Asia/Shanghai");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInShanghai = ZonedDateTime.of(localtDateAndTime, america );
return dateAndTimeInShanghai;
}
//这一个月有多少天
public static int getLengthOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
return yearMonth.lengthOfMonth();
}
//这一年有多少天
public static int getLengthOfYear(int year) {
Year year1 = Year.of(year);
return year1.length();
}
//是否闰年
public static boolean isLeapYear(int year) {
Year year1 = Year.of(year);
return year1.isLeap();
}
public static int gapMonths(LocalDate date1, LocalDate date2){
Period periodToNextJavaRelease = Period.between(date1, date2);
return periodToNextJavaRelease.getMonths();
}
public static int gapDays(LocalDate date1, LocalDate date2){
Period periodToNextJavaRelease = Period.between(date1, date2);
return periodToNextJavaRelease.getDays();
}
public static Date localDate2Date(LocalDate date){
ZoneId zone = ZoneId.systemDefault();
Instant instant = date.atStartOfDay().atZone(zone).toInstant();
java.util.Date da = Date.from(instant);
return da;
}
public static LocalDate date2LocalDate(java.util.Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate= localDateTime.toLocalDate();
return localDate;
}
public static Date localDateTime2Date(LocalDateTime date){
ZoneId zone = ZoneId.systemDefault();
Instant instant = date.atZone(zone).toInstant();
java.util.Date da = Date.from(instant);
return da;
}
public static LocalDateTime date2LocalDateTime(java.util.Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime;
}
public static LocalDate format2LocalDate(String dayAfterTommorrow) {
//eg: String dayAfterTommorrow = "20180205";
LocalDate formatted = LocalDate.parse(dayAfterTommorrow,
DateTimeFormatter.BASIC_ISO_DATE);
return formatted;
}
public static void formatExample() {
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//日期转字符串
String str = date.format(format1);
System.out.println("日期转换为字符串:"+str);
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//字符串转日期
LocalDate date2 = LocalDate.parse(str,format2);
System.out.println("日期类型:"+date2);
}
}