java 8中新的日期和時(shí)間API
使用LocalDate和LocalTime
LocalDate的實(shí)例是一個(gè)不可變對(duì)象,它只提供了簡單的日期,并不含當(dāng)天的時(shí)間信息.另外,它也不附帶任何與時(shí)區(qū)相關(guān)的信息
LocalDate實(shí)例提供了多種方法來讀取常用的值,比如年份,月份,星期幾等
LocalDate date = LocalDate.of(2014,3,18);
int year = date.getYear();
Month month = date.getMonth();
int day = date.getDayOfMonth();
DayOfWeek dow = date.getDayOfWeek();
int len = date.lengthOfMonth();
boolean leap = date.isLeapYear();
//獲取當(dāng)前的日期
LocalDate today = LocalDate.now();
你還可以通過傳遞一個(gè)TemporalField參數(shù)給get方法拿到童謠給您的信息.
int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);
類似的,一天中的時(shí)間,比如:13:45:20,可以使用LocalTime類表示
LocalTime time = LocalTime.of(13,45,20);
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
兩者都可以通過解析代表他們的字符串創(chuàng)建;
LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time = LocalTime.parse("13:45:20");
合并日期和時(shí)間
這個(gè)復(fù)合類名叫LocalDateTime,是LocalDate和LocalTime的合體.它同時(shí)表示了日期和時(shí)間,但不帶有時(shí)區(qū)信息
//2014-03-18T13:45:20
LocalDateTime dt1 = LocalDateTime.of(2014,Month.MARCH,18,13,45,20);
LocalDateTime dt2 = LocalDateTime.of(date,time);
LocalDateTime dt3 = date.atTime(13,45,20);
LocalDateTime dt4 = date.atTime(time);
LocalDateTime dt5 = date.atTime(date);
可以從LocalDateTime中提取LocalDate或者LocalTime
LocalDate date1 = dt1.toLocalDate();
LocalTime time1 = dt1.toLocalTime();
機(jī)器的日期和時(shí)間格式
java.time.Instant類對(duì)時(shí)間建模的方式,基本上是以Unix元年時(shí)間(1970年1月1日午夜時(shí)分)開始所經(jīng)歷的秒數(shù)進(jìn)行計(jì)算
Instant instant = Instant.ofEpochSecond(3);//1970-01-01T00:00:03Z
Instant.ofEpochSecond(3, 1_000_000_000);//1970-01-01T00:00:04Z 第二參數(shù)以納秒為單位的參數(shù)值
Duration或Period
Duration類的靜態(tài)工廠方法between
Duration.between(time, time2);
Duration.between(dateTime1, dateTime2);
Duration.between(instant1, instant12);
由于LocalDateTime和Instant是為不同的目的而設(shè)計(jì)的,一個(gè)是為了便于人閱讀使用,另一個(gè)是為了便于機(jī)器處理.所以不能將二者混用
此外,由于Duration類主要用于以秒和納秒衡量時(shí)間的長短,不能僅向between方法傳遞一個(gè)LocalDate對(duì)象做參數(shù)
Period類的between,可以使用得到兩個(gè)LocalDate之間的時(shí)長
Period between = Period.between(LocalDate.of(2014, 3, 8), LocalDate.of(2014, 3, 18));
創(chuàng)建Duration和Period對(duì)象
Duration.ofMillis(3);
Duration.of(3, ChronoUnit.MINUTES);
Period.ofDays(10);
Period.ofWeeks(3);
Period.of(2, 6, 1);
操縱,解析和格式化日期
如果已經(jīng)有一個(gè)LocalDate對(duì)象,想要?jiǎng)?chuàng)建它的一個(gè)修改版,最直接也最簡單的方法是使用withAttribute方法.
LocalDate of = LocalDate.of(2014, 3, 18);
LocalDate localDate = of.withYear(2011);
LocalDate localDate1 = localDate.withDayOfMonth(25);//2011-03-25
localDate1.with(ChronoField.MONTH_OF_YEAR,9);//2011-09-25
LocalDate localDate2 = of.plusWeeks(1);//2014-03-25
LocalDate localDate3 = localDate2.minusYears(3);//2011-03-25
localDate3.plus(6,ChronoUnit.MONTHS);//2011-09-25
使用TemporalAdjuster
LocalDate of = LocalDate.of(2014, 3, 18);
LocalDate with = of.with(nextOrSame(DayOfWeek.SUNDAY));//2014-03-23
LocalDate with1 = with.with(lastDayOfMonth());//2014-03-31
方法名 | 描述 |
---|---|
dayOfWeekInMonth | 創(chuàng)建一個(gè)新的日期,它的值為同一個(gè)月中每一周的第幾天 |
firstDayOfMonth | 當(dāng)月第一天 |
firstDayOfNextMonth | 下個(gè)月第一天 |
firstDayOfNextYear | 明年的第一天 |
firstDayOfYear | |
lastDayOfMonth | |
lastDayOfNextMonth | |
lastDayOfNextYear | |
lastDayOfYear | |
lastInMonth | 它的值為同一個(gè)月中,最后一個(gè)符合星期幾要求的值 |
next/previous | 將其值設(shè)定為日期調(diào)整后或者調(diào)整前,第一個(gè)符合指定星期幾要求的日期 |
nextOrSame/previousOrSame | 將其值設(shè)定為日期調(diào)整后或者調(diào)整前,第一個(gè)符合指定星期幾要求的日期,如果該日期已經(jīng)符合要求,直接返回該對(duì)象 |
打印輸出及解析日期-時(shí)間對(duì)象
LocalDate of1 = LocalDate.of(2014, 3, 18);
String format = of1.format(DateTimeFormatter.BASIC_ISO_DATE);//20140318
String format1 = of1.format(DateTimeFormatter.ISO_LOCAL_DATE);//2014-03-18
可以使用工廠方法parse達(dá)到重創(chuàng)改日期對(duì)象
LocalDate parse = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);//2014-03-18
LocalDate parse1 = LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);//2014-03-18
按照摸個(gè)模式創(chuàng)建DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate of1 = LocalDate.of(2014, 3, 18);
String format = of1.format(formatter);
LocalDate parse = LocalDate.parse(format, formatter);