Java 日期 API

先娶国王后取经發表於2024-11-11

JDK8之前

日期與時間戳之間的轉換

public class Test {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("date = " + date); // date = Sun Sep 26 14:48:52 CST 2021
        Date date1 = new Date(1632638970000L);
        System.out.println("date1 = " + date1); // date1 = Sun Sep 26 14:49:30 CST 2021

        long time = date.getTime();
        System.out.println("time = " + time); // time = 1632639026166

        date.setTime(1632638970000L);
        System.out.println("date = " + date); // date = Sun Sep 26 14:49:30 CST 2021
    }
}

日曆獲取年月日欄位

public class Test {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        int milliSecond = calendar.get(Calendar.MILLISECOND);
        // 2021-9-26 15:14:6:16
        System.out.print(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + ":" + milliSecond);
    }
}

日期格式化

public class Test {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        // 將日期物件格式化為字串
        String format = sdf.format(date);
        System.out.println("format = " + format); // format = 2021-09-26 16:02:17
        // 將字串解析為日期物件
        Date parse = sdf.parse(format);
        System.out.println("parse = " + parse); // parse = Sun Sep 26 16:02:17 CST 2021
    }
}

JDK8之後

之前存在的問題

  1. Java 1.0 中包含了一個 Date 類,但是它的大多數方法已經在 Java 1.1 中引入 Calendar 類的時候被廢棄了。
  2. 格式化只對 Date 有效,Calendar 則不行。
  3. 它們不是執行緒安全的,不能處理閏秒等

注:API 功能更加強大,但好像用的更多的還是老版本寫法

本地日期時間

  • Instant 是帶時區的(以UTC為準),用來替換以前的 Date
  • LocalDateTIme 是不帶時區(以本地時區為準)的,用來替換以前的 Calendar
public class Test {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println("now = " + now); // now = 2021-09-26
        LocalTime now1 = LocalTime.now();
        System.out.println("now1 = " + now1); // now1 = 20:02:32.659
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("now2 = " + now2); // now2 = 2021-09-26T20:02:32.660
        
        
        Instant now = Instant.now(); 
        System.out.println("now = " + now); // now = 2021-09-27T01:54:39.351Z
        LocalDateTime now1 = LocalDateTime.now();
        System.out.println("now1 = " + now1); // now1 = 2021-09-27T09:54:39.401
        
        
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        System.out.println("獲取年份:" + year);
        int monthValue = now.getMonthValue();
        System.out.println("獲取月份:" + monthValue);
        int dayOfMonth = now.getDayOfMonth();
        System.out.println("獲取天數:" + dayOfMonth);
        int dayOfYear = now.getDayOfYear();
        System.out.println("獲取一年中的第" + dayOfYear + "天");
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        System.out.println("獲取星期" + dayOfWeek.getValue());
        int hour = now.getHour();
        System.out.println("獲取小時:" + hour);
        int minute = now.getMinute();
        System.out.println("獲取分鐘:" + minute);
        int second = now.getSecond();
        System.out.println("獲取秒:" + second);
        int nano = now.getNano();
        System.out.println("獲取納秒:" + nano);
    }
}

日期格式化

public class Test {
    public static void main(String[] args) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 將日期格式化為字串
        String format = df.format(LocalDateTime.now());
        System.out.println("format = " + format); // format = 2021-09-27 13:55:51
        // 將字串解析為日期
        TemporalAccessor parse = df.parse("2011-11-11 11:11:11");
        LocalDateTime from = LocalDateTime.from(parse);
        System.out.println("from = " + from); // from = 2011-11-11T11:11:11
    }
}

相關文章