jdk8的datetime時(shí)間函數(shù)使用示例
JDK8已發(fā)布,寫了一個(gè)datetime時(shí)間函數(shù)使用方法的小示例
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) {
// 創(chuàng)建時(shí)間對(duì)象
LocalDateTime timePoint = LocalDateTime.now(); // 當(dāng)前時(shí)間
System.out.println("--當(dāng)前時(shí)間----");
System.out.println(timePoint);
System.out.println("");
System.out.println("--自定義時(shí)間----");
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("--獲取時(shí)間的各個(gè)部分----");
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("---設(shè)置并返回新的時(shí)間對(duì)象---");
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("---使用時(shí)間調(diào)整函數(shù)---");
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("---截?cái)鄷r(shí)間精確位--");
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("---使用時(shí)區(qū)---");
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("---時(shí)間上使用時(shí)區(qū)偏移---");
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("---時(shí)間加減---");
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("");
}
}
運(yùn)行結(jié)果
--當(dāng)前時(shí)間----
2014-03-25T17:03:40.553
--自定義時(shí)間----
2012-12-12
1970-05-31
17:18
10:15:30
--獲取時(shí)間的各個(gè)部分----
2014-03-25
MARCH
25
40
---設(shè)置并返回新的時(shí)間對(duì)象---
2010-03-10T17:03:40.553
---再加3周---
2010-04-21T17:03:40.553
---使用時(shí)間調(diào)整函數(shù)---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583
---截?cái)鄷r(shí)間精確位--
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40
---使用時(shí)區(qū)---
2014-03-25T09:03:40.583Z[UTC]
+08:00
---時(shí)間上使用時(shí)區(qū)偏移---
17:03:40.585+08:00
11:03:40.585+02:00
---時(shí)間加減---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586
相關(guān)文章
Java的微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例
這篇文章主要介紹了Java微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例,注意一下json-lib所需要的jar包,需要的朋友可以參考下2016-02-02SpringBoot Bean被加載時(shí)進(jìn)行控制
很多時(shí)候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個(gè)Bean,這就是Bean的加載控制,一般我們可以通過編程式或注解式兩種不同的方式來完成Bean的加載控制2023-02-02Java中短路運(yùn)算符與邏輯運(yùn)算符示例詳解
這篇文章主要給大家介紹了關(guān)于Java中短路運(yùn)算符與邏輯運(yùn)算符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01maven插件maven-assembly-plugin打包歸納文件zip/tar使用
java項(xiàng)目運(yùn)行的文件需要jar或者war格式,同時(shí)還需要使用Java命令,本文主要介紹了maven插件maven-assembly-plugin打包歸納文件zip/tar使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02教你怎么用SpringBoot+Mybati-Plus快速搭建代碼
Mybatis自身通過了逆向工程來幫助我們快速生成代碼,但Mybatis-plus卻更加強(qiáng)大,不僅僅可以生成dao,pojo,mapper,還有基本的controller和service層代碼,接下來我們來寫一個(gè)簡單的人門案例是看看如何mybatis-plus是怎么實(shí)現(xiàn)的,需要的朋友可以參考下2021-06-06java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法
這篇文章主要介紹了java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作的解決方法,需要的朋友可以參考下2014-03-03