package com.jvwl.test.jdk8;
import java.time.Clock;
import java.time.DayOfWeek;
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.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import org.testng.annotations.Test;
/**
* Created by jervalj on 2018-08-22
*/
public class LocalDateTimeTest {
/**
* LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日。 也可以访问其他日期字段,例如日期,星期几和星期。 例如,值“2007年10月2日”可存储在LocalDate 。 该类不存储或表示时间或时区。 相反,它是日期的描述,用于生日。
* 它不能代表时间线上的即时信息,而没有附加信息,如偏移或时区。
*/
@Test
public void localDateTest() {
LocalDate localDate = null;
System.out.println(LocalDate.now());// Get current date 2018-08-22
localDate = LocalDate.parse("2018-08-22");// Default pattern: ISO yyyy-MM-dd
localDate = LocalDate.parse("2018 8 22", DateTimeFormatter.ofPattern("yyyy M dd"));// Custom DateTimeFormatter
System.out.println(localDate);// 2018-08-22
System.out.println(LocalDate.of(2018, 8, 20));// Set date 2018-08-20
System.out.println(localDate.with(TemporalAdjusters.firstDayOfMonth()));// 2018-08-01
System.out.println(localDate.with(TemporalAdjusters.firstDayOfNextMonth()));// 2018-09-01
System.out.println(localDate.with(TemporalAdjusters.firstDayOfNextYear()));// 2019-01-01
System.out.println(localDate.with(TemporalAdjusters.firstDayOfYear()));// 2018-01-01
System.out.println(localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));// The first Monday in this month:2018-08-06
System.out.println(localDate.with(TemporalAdjusters.lastDayOfMonth()));// 2018-08-31
System.out.println(localDate.with(TemporalAdjusters.lastDayOfYear()));// 2018-12-31
System.out.println(localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.THURSDAY)));// 2018-08-30
System.out.println(localDate.with(TemporalAdjusters.next(DayOfWeek.THURSDAY)));// 2018-08-23
System.out.println(localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY)));// 2018-08-23
System.out.println(localDate.with(TemporalAdjusters.previous(DayOfWeek.THURSDAY)));// 2018-08-16
System.out.println(localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY)));// 2018-08-16
System.out.println(localDate.with(ChronoField.DAY_OF_MONTH, 5));// 2018-08-05
System.out.println(localDate.withDayOfMonth(3));// 2018-08-03
System.out.println(localDate.withDayOfYear(50));// 2018-02-19
System.out.println(localDate.withMonth(3));// 2018-03-22
System.out.println(localDate.withYear(2019));// 2019-08-22
System.out.println(localDate.plus(Period.ofWeeks(1)));// 2018-08-29
System.out.println(localDate.plus(4, ChronoUnit.DAYS));// 2018-08-26
System.out.println(localDate.plusDays(5));// 2018-08-27
System.out.println(localDate.plusMonths(4));// 2018-12-22
System.out.println(localDate.plusWeeks(3));// 2018-09-12
System.out.println(localDate.plusYears(2));// 2020-08-22
System.out.println(localDate.plus(Period.ofDays(3)));// 2018-08-25
System.out.println(localDate.plus(2, ChronoUnit.MONTHS));// 2018-10-22
System.out.println(localDate.plusDays(5));// 2018-08-27
System.out.println(localDate.plusMonths(4));// 2018-12-22
System.out.println(localDate.plusWeeks(3));// 2018-09-12
System.out.println(localDate.plusYears(2));// 2020-08-22
// Get first day of month
System.out.println(localDate.with(TemporalAdjusters.firstDayOfMonth()));// 2018-08-01
System.out.println(localDate.withDayOfMonth(1));// 2018-08-01
// Thanks Giving day
System.out.println(localDate.withMonth(11).with(TemporalAdjusters.lastInMonth(DayOfWeek.THURSDAY)));// 2018-11-29
// Print year, month, day.
// Year2018 Month8 Day22
System.out.printf("Year%d Month%d Day%d %n", localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth());
// Check two data are same or not
System.out.println(localDate.equals(LocalDate.of(2018, 8, 22)));// true
System.out.println(localDate.equals(LocalDate.from(LocalDateTime.of(2018, 8, 22, 14, 56))));// true
System.out.println(localDate.isLeapYear());// false
}
/**
* LocalTime是一个不可变的日期时间对象,代表一个时间,通常被看作是小时 - 秒。 时间表示为纳秒精度。 例如,值“13:45.30.123456789”可以存储在LocalTime 。 它不存储或表示日期或时区。 相反,它是在挂钟上看到的当地时间的描述。
* 它不能代表时间线上的即时信息,而没有附加信息,如偏移或时区。
*/
@Test
public void localTimeTest() {
System.out.println(LocalTime.now());// 14:35:24.771
System.out.println(LocalTime.now().withNano(0));// Remove 771: 14:35:24
System.out.println(LocalTime.of(13, 35));// 13:35
System.out.println(LocalTime.of(0, 0, 0));// 00:00
System.out.println(LocalTime.of(2, 3, 56, 243));// 02:03:56.000000243
System.out.println(LocalTime.parse("18:21"));// 18:21
System.out.println(LocalTime.parse("18:21:34"));// 18:21:34
System.out.println(LocalTime.parse("18:21:34.456"));// 18:21:34.456
System.out.println(LocalTime.parse("13-23-45 345", DateTimeFormatter.ofPattern("HH-mm-ss nnn")));// 13:23:45.000000345
System.out.println(LocalTime.parse("18:21").plusHours(1));// 19:21
}
/**
* LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒。 也可以访问其他日期和时间字段,例如日期,星期几和星期。 时间表示为纳秒精度。
* 例如,值“2007年10月2日在13:45.30.123456789”可以存储在LocalDateTime 。 该类不存储或表示时区。 相反,它是对日子的描述,如用于生日,结合当地时间在挂钟上看到的。 它不能代表时间线上的即时信息,而没有附加信息,如偏移或时区。
*/
@Test
public void localDateTime() {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// Same usage with LocalDate and LocalTime
// .......
}
@Test
public void clock() {
Clock clock = Clock.systemUTC();
System.out.println(clock.instant());// 2018-08-24T05:39:24.856Z
System.out.println(clock.millis());// 1535089164898
System.out.println(new Date().getTime());// 1535089164898
Instant timestamp = Instant.now();
System.out.println(timestamp.toEpochMilli());// 1535089164898
}
@Test
public void instant() {
Instant timestamp = Instant.now();
System.out.println(timestamp);
System.out.println(timestamp.toEpochMilli());
}
@Test
public void period() {
LocalDate today = LocalDate.now();
LocalDate birth = LocalDate.of(2018, Month.APRIL, 26);
Period age = Period.between(birth, today);
System.out.printf("Year:%d,Month:%d,Day:%d %n", age.getYears(), age.getMonths(), age.getDays());
}
@Test
public void zonedDateTime() {
Instant instant = Instant.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println("CN:" + ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")).format(formatter));
System.out.println("UK:" + ZonedDateTime.ofInstant(instant, ZoneId.of("Europe/London")).format(formatter));
System.out.println("US:" + ZonedDateTime.ofInstant(instant, ZoneId.of("America/Los_Angeles")).format(formatter));
}
@Test
public void monthDay() {
LocalDate today = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(2018, 04, 26);
dateOfBirth = LocalDate.of(2018, 8, 24);
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);
if (currentMonthDay.equals(birthday)) {
System.out.println("happy birthday!");
} else {
System.out.println("Happy everyday!");
}
}
@Test
public void yearMonth() {
LocalDate today = LocalDate.now();
LocalDate dateMonth = LocalDate.of(2018, 04, 26);
dateMonth = LocalDate.of(2018, 8, 24);
YearMonth expireMonth = YearMonth.of(dateMonth.getYear(), dateMonth.getMonth());
YearMonth currentMonth = YearMonth.from(today);
if (currentMonth.equals(expireMonth)) {
System.out.println("It is in available p!");
} else {
System.out.println("It is NOT under reportMonth!");
}
}
}