「JDK8」LocalDateTime和LocalDate等API
LocalDate API
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class LocalDateTest {
static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static void main(String[] args) {
//获得当前时间
LocalDate today = LocalDate.now();
//获取年份
int year = today.getYear(); //2022
//获取月份
int month = today.getMonthValue(); //1~12
//获取日期
int day = today.getDayOfMonth(); //1~31
//一年中的第几天
int dayOfYear = today.getDayOfYear();
//DayOfWeek是代表一周中7天的枚举 - 星期一,星期二,星期三,星期四,星期五,星期六和星期日
DayOfWeek week = today.getDayOfWeek();
int weekValue = week.getValue(); //星期三, 值为3
//获取当前时间前/后X天的日期
//获取前一天的日期
LocalDate data = today.plusDays(-1);
//获取后一天的日期
LocalDate data1 = today.plusDays(+1);
//获取本周周一的日期
LocalDate weekOfFirstDay = today.with(DayOfWeek.MONDAY);
//获取本月第一天的日期
LocalDate monthOfFirstDay = today.with(TemporalAdjusters.firstDayOfMonth());
//获取月份总天数
int totalDays = today.withMonth(today.getMonthValue()).lengthOfMonth();
//获取年份的第一天
LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear());
//是否闰年
boolean leapYear = today.isLeapYear();
System.out.println(leapYear);
//一天的开始时间
LocalDateTime localDateTime = today.atStartOfDay();
//日期结合时间
LocalDateTime localDateTime1 = today.atTime(LocalTime.parse("12:10:20"));
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = today.atTime(8, 3);
System.out.println(localDateTime2);
//指定年份, 这里表示2010年
LocalDate localDate3 = today.withYear(2010);
System.out.println(localDate3);
//指定月份, 这里表示12月份
LocalDate localDate4 = today.withMonth(12);
System.out.println(localDate4);
LocalDate localDate5 = today.with(ChronoField.MONTH_OF_YEAR, 2);
System.out.println(localDate5);
//指定日期, 这里表示25号
LocalDate localDate = today.withDayOfMonth(25);
System.out.println(localDate);
//自定义年月日的LocalDate
LocalDate localDate6 = LocalDate.of(2022, 12, 1);
System.out.println(localDate6);
//String转LocalDate
LocalDate date = LocalDate.parse("2020-01-01");
LocalDate date1 = LocalDate.parse("2020-01-01", dateTimeFormatter);
//LocalDate转String
String dateStr = today.format(dateTimeFormatter);
System.out.println(dateStr);
//获取两个时间间隔中的每一天
List<String> list = new ArrayList();
LocalDate startDate = LocalDate.parse("2020-01-01");
LocalDate endDate = LocalDate.parse("2020-01-10");
long distance = ChronoUnit.DAYS.between(startDate, endDate); //传入开始时间和结束时间
if (distance > 0) {
Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString()));
}
System.out.println(list);
//比较两个日期的大小
//判断localDate1是否和localDate2相等,返回布尔值
LocalDate localDate1 = LocalDate.parse("2020-01-01");
LocalDate localDate2 = LocalDate.parse("2020-01-01");
boolean equal = localDate1.isEqual(localDate2);
System.out.println(equal);
//判断localDate1是否在localDate2之前,返回布尔值
boolean before = localDate1.isBefore(localDate2);
System.out.println(before);
//判断localDate1是否在localDate2之后,返回布尔值
boolean after = localDate1.isAfter(localDate2);
System.out.println(after);
//两个时间间隔天数
LocalDate date4 = LocalDate.of(2022, 4, 30);
LocalDate date5 = LocalDate.of(2022, 4, 21);
int countdown = Math.toIntExact(date4.toEpochDay() - date5.toEpochDay());
System.out.println(countdown); //date4 > date5为正数
}
}
LocalDateTime API
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeTest {
static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
//获取当前时间
LocalDateTime today = LocalDateTime.now();
//年
int year = today.getYear();
//月
int month = today.getMonthValue();
//日
int day = today.getDayOfMonth();
//时
int hour = today.getHour();
//分
int minute = today.getMinute();
//秒
int second = today.getSecond();
//LocalDateTime和String相互转换
//LocalDateTime转String
String formatResult = dateTimeFormatter.format(today);
//String转LocalDateTime
LocalDateTime parseDateTime = LocalDateTime.parse("2022-06-22 10:12:20", dateTimeFormatter);
}
}
LocalTime API
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
public class LocalTimeTest {
public static void main(String[] args) {
//获取当前的时间, 时分秒
LocalTime today = LocalTime.now();
//时
int hour = today.getHour();
//分
int minute = today.getMinute();
//秒
int second = today.getSecond();
//纳秒
int nano = today.getNano();
//获取指定Field的值
today.getLong(ChronoField.HOUR_OF_DAY);
//截断
//ChronoUnit.HOURS 14:54:12.317 -> 14:00
//ChronoUnit.MINUTES 14:54:12.317 -> 14:54
//ChronoUnit.SECONDS 14:54:12.317 -> 14:54:12
LocalTime localTime = today.truncatedTo(ChronoUnit.SECONDS);
System.out.println(localTime);
//将时间LocalTime结合到LocalDate中
LocalDateTime localDateTime = today.atDate(LocalDate.parse("2022-06-20"));
System.out.println(localDateTime);
}
}