1.Local Data-Time API:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
public class Main {
public static void main(String args[]){
Main test = new Main();
test.testLocalDateTime();
}
public void testLocalDateTime(){
// Get the current date and time
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentTime);
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date1: " + date1);
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int seconds = currentTime.getSecond();
System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("date2: " + date2);
//12 december 2014
LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
System.out.println("date3: " + date3);
//22 hour 15 minutes
LocalTime date4 = LocalTime.of(22, 15);
System.out.println("date4: " + date4);
//parse a string
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date5: " + date5);
}
}
2.Zoned Date-Time API:
时区相关
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testZonedDateTime();
}
public void testZonedDateTime(){
// Get the current date and time
ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+05:30[Asia/Karachi]");
System.out.println("date1: " + date1);
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("ZoneId: " + id);
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("CurrentZone: " + currentZone);
}
}
3.Chrono Units Enum:
日期加减法(重点注意月份的加减)
加1策略应该是如果下个月没有对应的日期,那么返回月末最后一天,比如2017-1-31加1月返回2017-2-28。
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String args[]){
Main java8tester = new Main();
java8tester.testChromoUnits();
}
public void testChromoUnits(){
LocalDate today = LocalDate.of(2017, Month.JANUARY,31);
System.out.println("Current date: " + today);
//add 1 week to the current date
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Next week: " + nextWeek);
//主要月数之间的加法有区别,28,29,30,31等好麻烦
LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
LocalDate nextMonth1 = today.plus(2, ChronoUnit.MONTHS);
LocalDate nextMonth2 = today.plus(3, ChronoUnit.MONTHS);
System.out.println("Next month: " + nextMonth);
System.out.println("Next 2 month: " + nextMonth1);
System.out.println("Next 3 month: " + nextMonth2);
//add 1 year to the current date
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("Next year: " + nextYear);
//add 10 years to the current date
LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
System.out.println("Date after ten year: " + nextDecade);
}
}
4.Period & Duration
主要处理时间的差异:
1.Period − It deals with date based amount of time.
2.Duration − It deals with time based amount of time.
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;
import java.time.Period;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testPeriod();
java8tester.testDuration();
}
public void testPeriod(){
//Get the current date
LocalDate date1 = LocalDate.now();
System.out.println("Current date: " + date1);
//add 1 month to the current date
LocalDate date2 = date1.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + date2);
Period period = Period.between(date2, date1);
System.out.println("Period: " + period);
}
public void testDuration(){
LocalTime time1 = LocalTime.now();
Duration twoHours = Duration.ofHours(2);
LocalTime time2 = time1.plus(twoHours);
Duration duration = Duration.between(time1, time2);
System.out.println("Duration: " + duration);
}
}
结果:
Current date: 2017-04-02
Next month: 2017-05-02
Period: P-1M
Duration: PT2H
5.Temporal Adjusters
TemporalAdjuster is used to perform the date mathematics. For example, get the “Second Saturday of the Month” or “Next Tuesday”.
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.DayOfWeek;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testAdjusters();
}
public void testAdjusters(){
//Get the current date
LocalDate date1 = LocalDate.now();
System.out.println("Current date: " + date1);
//get the next tuesday,不管今天是不是SUNDAY
LocalDate nextSunday = date1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
//如果今天是SUNDAY,返回今天
LocalDate nextSunday1 = date1.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println("Next Sunday on : " + nextSunday);
System.out.println("Next Sunday on or same : " + nextSunday1);
//get the second saturday
LocalDate firstInYear = LocalDate.of(date1.getYear(),date1.getMonth(), 1);
System.out.println("firstinyear " + firstInYear);
LocalDate secondSaturday = firstInYear.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)).with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println("Second Saturday on : " + secondSaturday);
}
}
输出:
Current date: 2017-04-02
Next Sunday on : 2017-04-09
Next Sunday on or same : 2017-04-02
firstinyear 2017-04-01
Second Saturday on : 2017-04-15
6.Backward Compatibility
A toInstant() method is added to the original Date and Calendar objects, which can be used to convert them to the new Date-Time API(向前兼容). Use an ofInstant(Insant,ZoneId) method to get a LocalDateTime or ZonedDateTime object.
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testBackwardCompatability();
}
public void testBackwardCompatability(){
//Get the current date
Date currentDate = new Date();
System.out.println("Current date: " + currentDate);
//Get the instant of current date in terms of milliseconds
Instant now = currentDate.toInstant();
ZoneId currentZone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, currentZone);
System.out.println("Local date: " + localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, currentZone);
System.out.println("Zoned date: " + zonedDateTime);
}
}
关于format,请使用DateTimeFormatter
//能够自动将02等变为2,更humanize
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年M月d日");
System.out.println(date.format(df));