jdk8的datetime時光函數應用示例。本站提示廣大學習愛好者:(jdk8的datetime時光函數應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是jdk8的datetime時光函數應用示例正文
JDK8已宣布,寫了一個datetime時光函數應用辦法的小示例
package datetime;
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.previousOrSame;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class DataTimes {
public static void main(String[] args) {
// 創立時光對象
LocalDateTime timePoint = LocalDateTime.now(); // 以後時光
System.out.println("--以後時光----");
System.out.println(timePoint);
System.out.println("");
System.out.println("--自界說時光----");
System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
// values
System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
System.out.println(LocalTime.of(17, 18)); // the train I took home today
System.out.println(LocalTime.parse("10:15:30")); // From a String
System.out.println("");
System.out.println("--獲得時光的各個部門----");
System.out.println(timePoint.toLocalDate());
System.out.println(timePoint.getMonth());
System.out.println(timePoint.getDayOfMonth());
System.out.println(timePoint.getSecond());
System.out.println("");
System.out.println("---設置並前往新的時光對象---");
LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
System.out.println(thePast);
System.out.println("---再加3周---");
LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
ChronoUnit.WEEKS);
System.out.println(yetAnother);
System.out.println("");
System.out.println("---應用時光調劑函數---");
System.out.println(timePoint);
System.out.println(timePoint.with(lastDayOfMonth()));
System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
System.out.println(timePoint.with(LocalTime.now()));
System.out.println("");
System.out.println("---截斷時光准確位--");
System.out.println(timePoint);
LocalDateTime truncatedTimeToMinutes = timePoint
.truncatedTo(ChronoUnit.MINUTES);
System.out.println(truncatedTimeToMinutes);
LocalDateTime truncatedTimeToSeconds = timePoint
.truncatedTo(ChronoUnit.SECONDS);
System.out.println(truncatedTimeToSeconds);
System.out.println("");
System.out.println("---應用時區---");
ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
ZoneId.of("UTC+08:00"));
System.out
.println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
System.out.println(zoned_now.getOffset());
System.out.println("");
System.out.println("---時光上應用時區偏移---");
OffsetTime time = OffsetTime.now();
ZoneOffset offset = ZoneOffset.of("+02:00");
System.out.println(time);
System.out.println(time.withOffsetSameInstant(offset));
System.out.println("");
System.out.println("---時光加減---");
timePoint = LocalDateTime.now();
System.out.println(timePoint);
// 3 years, 2 months, 1 day
Period period1 = Period.of(3, 2, 1);
System.out.println(timePoint.plus(period1));
Duration duration = Duration.ofSeconds(3, 5);
System.out.println(timePoint.plus(duration));
Duration sixHours = Duration.between(
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
System.out.println(timePoint.plus(sixHours));
System.out.println("");
}
}
運轉成果
--以後時光----
2014-03-25T17:03:40.553
--自界說時光----
2012-12-12
1970-05-31
17:18
10:15:30
--獲得時光的各個部門----
2014-03-25
MARCH
25
40
---設置並前往新的時光對象---
2010-03-10T17:03:40.553
---再加3周---
2010-04-21T17:03:40.553
---應用時光調劑函數---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583
---截斷時光准確位--
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40
---應用時區---
2014-03-25T09:03:40.583Z[UTC]
+08:00
---時光上應用時區偏移---
17:03:40.585+08:00
11:03:40.585+02:00
---時光加減---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586