Java8中LocalDateTime相关类和方法都在这了【破涕为笑】
import org.junit.Test; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.Date; /** * @author Tuzi * @since 1.0.0 */ public class LocalDateTimeDemo { @Test public void testLocalDate() { // 获取LocalDate LocalDate now = LocalDate.now(); LocalDate localDate = LocalDate.of(1995, 10, 19); LocalDate localDate2 = LocalDate.parse("2017-02-22"); // 昨天 LocalDate yesterday = now.minusDays(1); // 上个月的今天 LocalDate localDate1 = now.minus(1, ChronoUnit.MONTHS); DayOfWeek dayOfWeek = localDate2.getDayOfWeek(); int dayOfMonth = localDate2.getDayOfMonth(); // 判断今年是不是闰年 boolean leapYear = now.isLeapYear(); // 获取这个月的第一天 LocalDate firstDayOfMonth = now.withDayOfMonth(1); LocalDate firstDayOfMonth2 = now.with(TemporalAdjusters.firstDayOfMonth()); // 判断今天是不是生日 LocalDate birthday = LocalDate.parse("1995-10-19"); MonthDay birthdayInMonthDay = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth()); MonthDay today = MonthDay.from(now); boolean todayIsMyBrithday = birthdayInMonthDay.equals(today); } @Test public void testLocalTime() { // 获取LocalTime LocalTime now = LocalTime.now(); // 16:39:45.252 LocalTime localTime = LocalTime.of(16, 40); LocalTime localTime1 = LocalTime.parse("16:40"); LocalTime nextHour = now.plus(1, ChronoUnit.HOURS); int hour = now.getHour(); int minute = now.getMinute(); boolean after = localTime.isAfter(now); // 常量 System.out.println(LocalTime.MAX); // 23:59:59.999999999 System.out.println(LocalTime.MIN); // 00:00 } @Test public void testLocalDateTime() { LocalDateTime now = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.parse("2017-11-06T16:48:15.801"); LocalDateTime localDateTime1 = LocalDateTime.of(2017, 11, 06, 16, 48, 15, 801); // 加减 LocalDateTime plusHours = now.plusHours(1); LocalDateTime minus = now.minus(2, ChronoUnit.DAYS); System.out.println(now.getDayOfMonth()); LocalDate localDate = localDateTime.toLocalDate(); LocalTime localTime = localDateTime.toLocalTime(); LocalDateTime localDateTime2 = LocalDateTime.from(localDate.atStartOfDay()); } @Test public void testDateTimeFormatter() { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("默认格式: " + now); System.out.println("自定义格式: " + now.format(dateTimeFormatter)); LocalDateTime.parse("2017-11-06 17:02:05", dateTimeFormatter); dateTimeFormatter.format(LocalDateTime.now()); } @Test public void testPeriod() { LocalDateTime now = LocalDateTime.now(); LocalDateTime plusTwoDay = now.plus(Period.ofDays(2)); long between = ChronoUnit.DAYS.between(now, plusTwoDay); } @Test public void transfer() { Date date = Date.from(Instant.now()); date.toInstant(); LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); LocalDateTime now = LocalDateTime.now(); Date.from(now.atZone(ZoneId.systemDefault()).toInstant()); LocalDate localDate = LocalDate.now(); Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); }