
抛弃Date,用LocalDateTime
package com.ifdom.date;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class DateMain {
public static void main(String[] args) {
dateCalculation();
}
public static void date2String() {
LocalDate now = LocalDate.now();
System.out.println(now);
String s = now.toString();
System.out.println(s);
}
public static void string2Date() {
String s = "2022-05-25";
LocalDate localDate = LocalDate.parse(s);
System.out.println(localDate);
LocalDate localDate1 = LocalDate.of(2022, 5, 25);
System.out.println(localDate1);
}
public static void datetime2String() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
String s = now.toString();
System.out.println(s);
LocalDateTime localDateTime = now.withNano(0);
System.out.println(localDateTime);
LocalDateTime localDate1 = LocalDateTime.of(2022, 5, 25, 1, 2);
System.out.println(localDate1);
}
public static void string2Datetime() {
String s = "2022-05-25 12:12:22";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(s, formatter);
System.out.println(localDateTime);
String s1 = "2022-05-25T10:15:31.581";
LocalDateTime localDateTime1 = LocalDateTime.parse(s1);
System.out.println(localDateTime1);
}
public static void dateFormatter() {
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String s = now.format(formatter);
System.out.println(s);
LocalDateTime now1 = LocalDateTime.now();
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String s1 = now1.format(formatter1);
System.out.println(s1);
}
public static void dateCalculation() {
LocalDate date1 = LocalDate.of(2022, 5, 15);
LocalDate date2 = LocalDate.of(2022, 3, 20);
boolean date1After = date1.isAfter(date2);
System.out.println(date1After);
LocalDate tomorrow = date1.plusDays(1);
System.out.println("明天:" + tomorrow);
LocalDate twoAfterDay = date1.plusDays(2);
System.out.println("后天:" + twoAfterDay);
LocalDate yesterday = date1.plusDays(-1);
System.out.println("昨天:" + yesterday);
LocalDate laterDay = date1.plusDays(-2);
System.out.println("前天:" + laterDay);
LocalDate tomorrowWeek = date1.plusWeeks(1);
System.out.println("下一周:" + tomorrowWeek);
LocalDate twoAfterWeek = date1.plusDays(2);
System.out.println("下下周:" + twoAfterWeek);
LocalDate yesterdayWeek = date1.plusWeeks(-1);
System.out.println("上一周:" + tomorrowWeek);
LocalDate laterWeek = date1.plusDays(-2);
System.out.println("上上周:" + twoAfterWeek);
Period period = Period.between(date1, date2);
int periodYears = period.getYears();
int periodMonths = period.getMonths();
int periodDays = period.getDays();
System.out.println("间隔 年:" + periodYears);
System.out.println("间隔 月:" + periodMonths);
System.out.println("间隔 日:" + periodDays);
long countDays = date1.toEpochDay() - date2.toEpochDay();
System.out.println("日期相差的总天数:" + countDays);
}
public static void dateSpecify() {
LocalDate today = LocalDate.now();
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
LocalDate nextDay = lastDayOfThisMonth.plusDays(1);
LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfYear());
LocalDate lastMondayOf2021 = LocalDate.parse("2021-12-31").with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
}
}