import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import org.junit.jupiter.api.Test;
public class LocalDateTimeTest {
@Test
void testLocalDateTime() {
System.out.println(LocalDate.now());
System.out.println(LocalTime.now());
System.out.println(LocalDateTime.now());
}
@Test
void test2toLocalDate() {
LocalDateTime now = LocalDateTime.now();
LocalDate localDate = now.toLocalDate();
LocalTime localTime = now.toLocalTime();
System.out.println(now + ", " + localDate + ", " + localTime);
}
@Test
void testOf() {
LocalDate localDate = LocalDate.of(2020, 8, 11);
LocalTime localTime = LocalTime.of(20, 32, 44);
LocalDateTime localDateTime = LocalDateTime.of(2020, 8, 11, 20, 32, 44);
LocalDateTime dateTime = LocalDateTime.of(localDate, localTime);
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
System.out.println(dateTime);
}
@Test
void testParse() {
LocalDateTime dateTime = LocalDateTime.parse("2020-08-11T20:32:44");
LocalDate localDate = LocalDate.parse("2020-08-11");
LocalTime localTime = LocalTime.parse("20:32:44");
System.out.println(dateTime);
System.out.println(localDate);
System.out.println(localTime);
}
@Test
void testDateTimeFormatter() {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(LocalDateTime.parse("2020/08/11 20:32:44", dateTimeFormatter));
System.out.println(dateTimeFormatter.format(LocalDateTime.now()));
}
@Test
void testCalc() {
LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(dt);
System.out.println(dt.plusYears(1).minusMonths(2).plusDays(3));
System.out.println(dt.minusHours(1).plusMinutes(2).minusSeconds(3));
System.out.println(dt.plusDays(6));
System.out.println(dt.plusSeconds(1));
}
@Test
void testAdjustmentDateTime() {
LocalDateTime dt = LocalDateTime.of(2019, 10, 31, 20, 30, 59);
System.out.println(dt);
System.out.println(dt.withDayOfMonth(29));
System.out.println(dt.withMonth(9));
}
@Test
void testComplexCalculation() {
LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
System.out.println(firstDay);
LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println(lastDay);
LocalDate firstDayOfNextMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(firstDayOfNextMonth);
LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstWeekday);
}
@Test
void testBefore() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime target = LocalDateTime.of(2020, 8, 31, 1, 1, 1);
System.out.println(now.isBefore(target));
System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 8, 8)));
System.out.println(LocalTime.now().isAfter(LocalTime.of(21, 1, 1)));
}
@Test
void testDuration() {
LocalDateTime startTime = LocalDateTime.of(2020, 5, 6, 17, 30, 15);
LocalDateTime endTime = LocalDateTime.of(2020, 8, 12, 21, 46, 30);
Duration duration1 = Duration.between(startTime, endTime);
System.out.println(duration1);
Duration duration2 = Duration.between(endTime, startTime);
System.out.println(duration2);
Period period = Period.between(startTime.toLocalDate(), endTime.toLocalDate());
System.out.println(period);
}
}
小结
- 默认严格按照ISO 8601规定的日期和时间格式进行打印;注意ISO 8601规定的日期和时间分隔符是T
- DateTimeFormatter自定义输出的格式
- LocalDateTime提供了对日期和时间进行加减的非常简单的链式调用
- 对日期和时间进行调整则使用withXxx()方法
- LocalDateTime还有一个通用的with()方法允许我们做更复杂的运算
- 要判断两个LocalDateTime的先后,可以使用isBefore()、isAfter()方法,对于LocalDate和LocalTime类似
- Duration表示两个时刻之间的时间间隔。另一个类似的Period表示两个日期之间的天数
资料参考