JAVA中常用時間類JDK8新增時間類-創(chuàng)新互聯(lián)

JDK8新增時間類 1、ZoneId時區(qū) (1)、方法
static SetgetAvailableZoneIds()    //	獲取Java中支持的所有時區(qū)
    static ZoneId systemDefault()               //	獲取系統(tǒng)默認時區(qū)
    static ZoneId of(String zoneId)             //	獲取一個指定時區(qū)
(2)、例子
public static void main(String[] args) {//  獲取java支持的所有時區(qū)
    SetzoneIds = ZoneId.getAvailableZoneIds();
    System.out.println(zoneIds.size());//目前版本有601個時區(qū) 版本不同時區(qū)個數不同
    System.out.println(zoneIds);       //[Asia/Aden, America/Cuiaba,....US/Pacific, Europe/Monaco]
    //  獲取系統(tǒng)默認時區(qū):Asia/Shanghai
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println(zoneId);
    //  獲取一個指定時區(qū)
    ZoneId of = ZoneId.of("SystemV/EST5EDT");
    System.out.println(of);             //SystemV/EST5EDT  目前不知道有啥用
}
2、Instant時間戳 (1)、方法
static Instant now()                        	//	獲取當前時間的Instant對象(標準時間)
static Instant ofxxxx(long epochMilli)      	//	根據 (秒/毫秒/納秒)獲取Instant對象
    ZonedDateTime atZone(ZoneId zone)           //	指定時區(qū)
    boolean isXxx(Instant otherInstant)         //	判系列的方法
    Instant minusXxx(long millisToSubtract)     //	減少時間系列的方法
    Instant plusXxx(long millisToSubtract)      //	增加時間系列的方法
(2)、例子
public static void main(String[] args) {//  獲取當前時間的Instant對象(標準時間)
    Instant nowTime = Instant.now();
    System.out.println(nowTime);                                    //  2023-01-17T11:19:49.729922200Z
    //  根據 (秒/毫秒/納秒)獲取Instant對象
    Instant instant1 = Instant.ofEpochMilli(1000L);                 //  參數是毫秒
    System.out.println(instant1);                                   //  1970-01-01T00:00:01Z
    Instant instant2 = Instant.ofEpochSecond(2L);                   //  參數是秒
    System.out.println(instant2);                                   //  1970-01-01T00:00:02Z
    Instant instant3 = Instant.ofEpochSecond(1L, 1000000000);   //參數是(秒,微妙)
    System.out.println(instant3);                                   //  1970-01-01T00:00:02Z
    //  指定時區(qū)  創(chuàng)建一個帶有時區(qū)的對象ZonedDateTime
    ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
    System.out.println(time);                                       //  2023-01-17T19:19:49.734909+08:00[Asia/Shanghai]
    //  判系列的方法
    Instant instant4 = Instant.ofEpochSecond(1L);
    Instant instant5 = Instant.ofEpochSecond(2L);
    //  判斷instant4時間是否在instant5之前
    boolean before = instant4.isBefore(instant5);
    System.out.println(before);                                     //  true
    //  判斷instant5時間是否在instant4之后
    boolean after = instant5.isAfter(instant4);
    System.out.println(after);                                      //  true
    //  Instant minusXxx(long millisToSubtract)     減少時間系列的方法
    Instant instant6 = Instant.ofEpochSecond(2L);
    System.out.println(instant6);                                   //1970-01-01T00:00:02Z
    //  減一秒
    Instant instant7 = instant6.minusSeconds(1);
    System.out.println(instant7);                                   //1970-01-01T00:00:01Z
    //  +2秒
    Instant instant8 = instant6.plusSeconds(2L);
    System.out.println(instant8);                                   //1970-01-01T00:00:04Z
}
3、ZonedDateTime類 (1)、方法
//  static ZonedDateTime now()                  獲取當前時間的ZonedDateTime對象
//  static ZonedDateTime ofXxxx(...)            獲取指定時間的ZonedDateTime對象
//  ZonedDateTime   withXxx(時間)                修改時間系列的方法
//  ZonedDateTime   minusXxx(時間)               減少時間系列的方法
//  ZonedDateTime   plusXxx(時間)                增加時間系列的方法
(2)、例子
public static void main(String[] args) {//  獲取一個  ZonedDateTime一個對象
    ZonedDateTime now = ZonedDateTime.now();
    System.out.println(now);                    //  2023-01-17T21:42:03.007920300+08:00[Asia/Shanghai]
    //  獲取指定時間的ZonedDateTime對象:
    ZonedDateTime zdt1 = ZonedDateTime.of(1999, 9, 9,
            9, 9, 9, 0,
            ZoneId.of("Asia/Shanghai"));
    System.out.println(zdt1);                   //  1999-09-09T09:09:09+08:00[Asia/Shanghai]
    //  獲取這個Asia/Shanghai時區(qū)的原始時間+60秒也就是+1分鐘
    Instant instant = Instant.ofEpochSecond(10L);
    ZoneId zid = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zdt2 = ZonedDateTime.ofInstant(instant, zid);
    System.out.println(zdt2);                  //  1970-01-01T08:00:10+08:00[Asia/Shanghai]
    
    //  修改zdt1的年
    ZonedDateTime zdt3 = zdt1.withYear(2000);
    System.out.println(zdt3);                  //   2000-09-09T09:09:09+08:00[Asia/Shanghai]
    //  減少一個月
    ZonedDateTime zdt4 = zdt1.minusMonths(1);
    System.out.println(zdt4);                  //   1999-08-09T09:09:09+08:00[Asia/Shanghai]
    //  增加一年
    ZonedDateTime zdt5 = zdt1.plusYears(1);    //   2000-09-09T09:09:09+08:00[Asia/Shanghai]
    System.out.println(zdt5);
}

細節(jié)

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網站制作、成都網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的威寧網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
  • JDK8新增的時間對象都是不可變的
  • 如果我們修改了,減少了,增加時間了
  • 那我們的調用者是不會發(fā)生改變的,產生一個新的時間
4、DateTimeFormatter (1)方法
//  public static DateTimeFormatter ofPattern(String pattern)       獲取格式化對象
//  public String format(TemporalAccessor temporal)                 按照指定格式進行格式化
(2)、例子
public static void main(String[] args) {DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
    //  獲取一個時區(qū)時間對象
    ZonedDateTime time1 = ZonedDateTime.now();
    //  解析,并格式化時間對象并輸出
    System.out.println(dtf1.format(time1));             //  2023-01-17 22:14:55
}
5、LocalDate、LocalTime、LocalDateTime (1)、方法

LocalDate:只能獲取年、月、日———LocalTime只能獲取時、分、秒——LocalDateTime都可以獲取到

在這里插入圖片描述

(2)、LocalDate例子
public static void main(String[] args) {//  創(chuàng)建一個LocalDate類型的現在時間的時間對象
    LocalDate time1 = LocalDate.now();
    System.out.println(time1);                              //  2023-01-17
    //  創(chuàng)建一個LocalDate類型的時間對象
    LocalDate time2 = LocalDate.of(2000, 12, 12);
    System.out.println(time2);                              //  2000-12-12

    // 獲取其中的年月日
    int year = time1.getYear();
    System.out.println("time1的年份:" + year);                // time1的年份:2023
    Month month = time1.getMonth();
    System.out.println("time1的月份:" + month);               // time1的月份:JANUARY
    System.out.println("time1的月份:" + month.getValue());    // time1的月份:1
    //  另外一種直接獲取到阿拉伯數字的方法
    int monthValue = time1.getMonthValue();
    System.out.println("time1的月份:" + monthValue);          // time1的月份:1
    int dayOfMonth = time1.getDayOfMonth();
    System.out.println("現在是這個月的第 " + dayOfMonth + "天");  // 現在是這個月的第 17天
    //  比較兩個時間的前后
    if (time1.isAfter(time2)) {  //time1在time2的時間后面
        System.out.println("time1在time2的時間后面");
    } else {System.out.println("time1在time2的時間前面");
    }
    //  time1的時間是2023-01-17
    //  修改時間with  年數修改為2000
    LocalDate withLocalDate = time1.withYear(2000);
    System.out.println(withLocalDate);                          //  2000-01-17
    //  減少時間minus   減少一個年
    LocalDate minusYears = time1.minusYears(1);                 //  2022-01-17
    System.out.println(minusYears);
    //  增加時間plus    增加一個月
    LocalDate plusMonths = time1.plusMonths(1);                 //  2023-02-17
    System.out.println(plusMonths);
}

其他LocalTime、LocalDateTime類似不舉例子了

6、時間間隔Duration、Period、ChronoUnit主要是計算兩個時間的時間間隔
  • Duration:用于計算兩個“時間”間隔(秒,納秒)
  • Period:用于計算兩個“日期”間隔(年、月、日)
  • ChronoUnit:用于計算兩個“日期”間隔
7、綜合小練習

計算一個人活了多少天了 例如:2000年11月11日;出生

public static void main(String[] args) throws ParseException {//  JDK7
    //  計算一個人活了多少天了 例如:2000年11月11日;出生
    //  獲取當前時間
    String birthday = "2000年11月11日";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    long now = System.currentTimeMillis();
    //  解析字符串獲取Date對象,然后獲取毫秒值
    Date parse = sdf.parse(birthday);
    long time = parse.getTime();
    //  計算天數
    long days = (now - time) / 1000 / 60 / 60 / 24;
    System.out.println(days);//8103

    //  JDK8
    LocalDate birthdayTime = LocalDate.of(2000, 11, 11);
    LocalDate nowTime = LocalDate.now();
    long lifeDays = ChronoUnit.DAYS.between(birthdayTime, nowTime);
    System.out.println(lifeDays);//8103
}

計算某一年是不是閏年:這次的判斷條件是,年數366天的是閏年,月份有29天的是閏年

public static void main(String[] args) {//  JDK7
    //  把時間設置為2000年3月1號
    Calendar instance = Calendar.getInstance();
    instance.set(2000, 2, 1);         //month的取值范圍是0~11
    instance.add(Calendar.DAY_OF_MONTH, -1);
    int dayOfMonth = instance.get(Calendar.DAY_OF_MONTH);
    System.out.println(dayOfMonth);                    //29
    //  JDK8
    LocalDate of = LocalDate.of(2000,3,1);
    LocalDate localDate = of.minusDays(1);
    int dayOfMonth1 = localDate.getDayOfMonth();
    System.out.println(dayOfMonth1);                    //29
    //  LocalDate有一個直接判斷閏年平年的方法
    boolean leapYear = of.isLeapYear();
    System.out.println(leapYear);//ture是閏年,false是平年
}

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧

網站欄目:JAVA中常用時間類JDK8新增時間類-創(chuàng)新互聯(lián)
標題網址:http://muchs.cn/article24/csjcce.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、品牌網站建設、App開發(fā)、移動網站建設、網站策劃、小程序開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站建設