欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jdk8的datetime時間函數(shù)使用示例

 更新時間:2014年03月26日 09:23:14   作者:  
這篇文章主要介紹了jdk8的datetime時間函數(shù)使用示例,需要的朋友可以參考下

JDK8已發(fā)布,寫了一個datetime時間函數(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)建時間對象
  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("---使用時間調(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("---截斷時間精確位--");
  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("---使用時區(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("---時間上使用時區(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("---時間加減---");
  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("");

 }
}

運行結(jié)果

復制代碼 代碼如下:

--當前時間----
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

---使用時間調(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

---截斷時間精確位--
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40

---使用時區(qū)---
2014-03-25T09:03:40.583Z[UTC]
+08:00

---時間上使用時區(qū)偏移---
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

相關文章

  • Java的微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例

    Java的微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例

    這篇文章主要介紹了Java微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例,注意一下json-lib所需要的jar包,需要的朋友可以參考下
    2016-02-02
  • SpringBoot Bean被加載時進行控制

    SpringBoot Bean被加載時進行控制

    很多時候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個Bean,這就是Bean的加載控制,一般我們可以通過編程式或注解式兩種不同的方式來完成Bean的加載控制
    2023-02-02
  • 全面解析Java設計模式之單例模式

    全面解析Java設計模式之單例模式

    這篇文章主要幫助大家全面解析Java設計模式之單例模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java中的異常Exception與處理方式詳解

    Java中的異常Exception與處理方式詳解

    這篇文章主要介紹了Java中的異常Exception與處理方式詳解, Java語言中,將程序執(zhí)行中發(fā)生的不正常情況稱為"異常"(開發(fā)過程中的語法錯誤和邏輯錯誤不是異常),需要的朋友可以參考下
    2024-01-01
  • 簡單了解java等待喚醒機制原理及使用

    簡單了解java等待喚醒機制原理及使用

    這篇文章主要介紹了簡單了解java等待喚醒機制原理及使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java中短路運算符與邏輯運算符示例詳解

    Java中短路運算符與邏輯運算符示例詳解

    這篇文章主要給大家介紹了關于Java中短路運算符與邏輯運算符的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • maven插件maven-assembly-plugin打包歸納文件zip/tar使用

    maven插件maven-assembly-plugin打包歸納文件zip/tar使用

    java項目運行的文件需要jar或者war格式,同時還需要使用Java命令,本文主要介紹了maven插件maven-assembly-plugin打包歸納文件zip/tar使用,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • 教你怎么用SpringBoot+Mybati-Plus快速搭建代碼

    教你怎么用SpringBoot+Mybati-Plus快速搭建代碼

    Mybatis自身通過了逆向工程來幫助我們快速生成代碼,但Mybatis-plus卻更加強大,不僅僅可以生成dao,pojo,mapper,還有基本的controller和service層代碼,接下來我們來寫一個簡單的人門案例是看看如何mybatis-plus是怎么實現(xiàn)的,需要的朋友可以參考下
    2021-06-06
  • java編譯時出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法

    java編譯時出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法

    這篇文章主要介紹了java編譯時出現(xiàn)使用了未經(jīng)檢查或不安全的操作的解決方法,需要的朋友可以參考下
    2014-03-03
  • java的引用類型的詳細介紹

    java的引用類型的詳細介紹

    在java中提供了4個級別的引用:強引用、軟引用、弱引用、虛引用。其中強引用FinalReference是default個飾符來修飾,其它3個級別均為public修飾
    2013-10-10

最新評論