大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java獲取當前時間的方法

Java獲取當前時間的方法

更新時間:2022-10-11 10:52:01 來源:動力節點 瀏覽2586次

1.在Java中獲取當前日期時間

以下是一些代碼片段,用于在 Java 中顯示當前日期時間。

對于java.time.LocalDate, 使用LocalDate.now()

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
  LocalDate localDate = LocalDate.now();
  System.out.println(dtf.format(localDate));            // 2021/03/22

對于java.time.localTime, 使用LocalTime.now()

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
  LocalTime localTime = LocalTime.now();
  System.out.println(dtf.format(localTime));            // 16:37:15

對于java.time.LocalDateTime, 使用LocalDateTime.now()

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  LocalDateTime now = LocalDateTime.now();
  System.out.println(dtf.format(now));                  //  2021/03/22 16:37:15

對于java.time.ZonedDateTime, 使用ZonedDateTime.now()

  // get current date-time, with system default time zone
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  ZonedDateTime now = ZonedDateTime.now();
  System.out.println(dtf.format(now));                  // 2021/03/22 16:37:15
  System.out.println(now.getOffset());                  // +08:00
  // get current date-time, with a specified time zone
  ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
  System.out.println(dtf.format(japanDateTime));        // 2021/03/22 17:37:15
  System.out.println(japanDateTime.getOffset());        // +09:00

對于java.time.Instant, 使用Instant.now()

  Instant now = Instant.now();
  // convert Instant to ZonedDateTime
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
  System.out.println(dtfDateTime.format(zonedDateTime));

對于java.util.Date, 使用new Date()

  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  Date date = new Date();
  System.out.println(dateFormat.format(date));           // 2021/03/22 16:37:15

對于java.util.Calendar, 使用Calendar.getInstance()

  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  Calendar cal = Calendar.getInstance();
  System.out.println(dateFormat.format(cal.getTime()));  // 2021/03/22 16:37:15

2.java.time.LocalDate

對于java.time.LocalDate, 用于LocalDate.now()獲取沒有時區的當前日期,并使用DateTimeFormatter.

package com.mkyong.app;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample {
  public static void main(String[] args) {
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
      LocalDate localDate = LocalDate.now();
      System.out.println(dtf.format(localDate));    // 2021/03/22
  }
}

輸出

  2021/03/22

3.java.time.LocalTime

對于java.time.LocalTime, 用于LocalDate.now()獲取沒有時區的當前時間,并使用DateTimeFormatter.

package com.mkyong.app;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeExample {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime = LocalTime.now();
        System.out.println(dtf.format(localTime));    // 16:37:15
    }
}

輸出

  16:37:15

4. java.time.LocalDateTime

對于java.time.LocalDateTime, 用于LocalDateTime.now()獲取沒有時區的當前日期時間,并使用DateTimeFormatter.

package com.mkyong.app;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));        //  2021/03/22 16:37:15
    }
}

輸出

  2021/03/22 16:37:15

5.java.time.ZonedDateTime

對于java.time.ZonedDateTime, 用于ZonedDateTime.now()獲取系統默認時區或指定時區的當前日期時間。

package com.mkyong.app;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample {
  public static void main(String[] args) {
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
      // Get default time zone
      System.out.println(ZoneOffset.systemDefault());         // Asia/Kuala_Lumpur
      System.out.println(OffsetDateTime.now().getOffset());   // +08:00
      // get current date time, with +08:00
      ZonedDateTime now = ZonedDateTime.now();
      System.out.println(dtf.format(now));                    // 2021/03/22 16:37:15
      System.out.println(now.getOffset());                    // +08:00
      // get get current date time, with +09:00
      ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
      System.out.println(dtf.format(japanDateTime));          // 2021/03/22 17:37:15
      System.out.println(japanDateTime.getOffset());          // +09:00
  }
}

輸出

Asia/Kuala_Lumpur
+08:00
2021/03/22 16:37:15
+08:00
2021/03/22 17:37:15
+09:00

6.java.time.Instant

對于java.time.Instant, 用于Instant.now()獲取自Unix 紀元時間(UTC 時間 1970 年 1 月 1 日午夜)以來經過的秒數,然后轉換為其他java.time.*日期時間類,如LocalDate,LocalDateTime和ZonedDateTime.

package com.mkyong.app;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class InstantExample {
  private static final DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("uuuu/MM/dd");
  private static final DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("HH:mm:ss");
  private static final DateTimeFormatter dtfDateTime = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  public static void main(String[] args) {
      // seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
      Instant now = Instant.now();
      // convert Instant to LocalDate
      LocalDate localDate = LocalDate.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDate.format(localDate));
      // convert Instant to localTime
      LocalTime localTime = LocalTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfTime.format(localTime));
      // convert Instant to LocalDateTime
      LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDateTime.format(localDateTime));
      // convert Instant to ZonedDateTime
      ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDateTime.format(zonedDateTime));
  }
}

輸出

2021/03/22
16:37:15
2021/03/22 16:37:15
2021/03/22 16:37:15

7. java.util.Date(舊版)

對于 legacy java.util.Date,使用new Date()ornew Date(System.currentTimeMillis()獲取當前日期時間,并使用SimpleDateFormat.

package com.mkyong.app;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample {
  public static void main(String[] args) {
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Date date = new Date();
      System.out.println(dateFormat.format(date));    // 2021/03/22 16:37:15
      // new Date() actually calls this new Date(long date)
      Date date2 = new Date(System.currentTimeMillis());
      System.out.println(dateFormat.format(date));    // 2021/03/22 16:37:15
  }
}

輸出

  2021/03/22 16:37:15
  2021/03/22 16:37:15

8. java.util.Calendar(舊版)

對于 legacy java.util.Calendar,用于Calendar.getInstance()獲取當前日期時間,并使用SimpleDateFormat.

package com.mkyong.app;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarExample {
  public static void main(String[] args) {
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Calendar cal = Calendar.getInstance();
      System.out.println(dateFormat.format(cal.getTime()));   // 2021/03/22 16:37:15
  }
}

輸出

 2021/03/22 16:37:15

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 99精品视频在线这里只有 | 中文字幕在线视频在线看 | 国产美女视频做爰 | 欧美aa一级 | 国产在线日韩 | 日本不卡高清免费v日本 | 99爱在线观看精品视频 | 国产亚洲综合一区在线 | 亚洲va欧美va国产 | 农村寡妇一级毛片免费播放 | 日本二三区 | 婷婷在线视频国产综合 | 国内精品久久久久影院不卡 | 四虎国产成人永久精品免费 | 久久综合五月天 | 成熟日本语热亚洲人 | 久久不射影院 | 国产成人精品久久综合 | 天天射天天爽 | 国产成人在线免费视频 | 亚洲欧美日韩在线精品2021 | 全免费毛片在线播放 | 九九影院理论片 | 亚洲国产成人99精品激情在线 | 亚洲欧美一区二区三区 | 亚洲狠狠婷婷综合久久久图片 | 欧美综合色 | 四虎4444hu4影视最新地址 | 欧美日本一本线在线观看 | 天天做天天爱天天一爽一毛片 | 最新亚洲精品国自产在线观看 | 亚洲视频一区在线播放 | 久久这里只有精品视频99 | 国产高清一区二区三区免费视频 | 夜夜操操| 精品一久久香蕉国产线看观 | 成人性视频免费网站 | 成人毛片18女人毛片 | 久久精品一区二区 | 日韩免费毛片视频 | 天天爽夜夜爽免费看 |