舊 API:位於 java.util 包中,裏面主要有 Date、Calendar、TimeZone 類 新 API:位於 java.time 包中,裏面主要有 LocalDateTime、ZonedDateTime、ZoneId 類java
[TOC]安全
Date
public static void main( String[] args ) { Date date = new Date(); System.out.println(date.getYear()+1900); // 2020 System.out.println(date.getMonth()+1); // 0~11 System.out.println(date.getDate()); // 9 System.out.println(date.toString()); // Mon Mar 09 15:33:13 CST 2020 System.out.println(date.toLocaleString()); // 2020-3-9 15:33:13 System.out.println(date.toGMTString()); // 9 Mar 2020 07:33:13 GMT }
年從1900開始算;月分從0開始算,因此須要加1eclipse
缺點:函數
- 沒法設置時區
- 沒法加減日期
- 沒法計算某天是星期幾
eclipse在jdk1.8下,這些函數基本都帶中劃線,不建議使用了spa
SimpleDateFormat
爲了能設置時區,有了SimpleDateFormat類線程
public static void main( String[] args ) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("y-M-d H:m:s"); // 2020-3-9 15:48:6 System.out.println(formatter.format(date)); formatter.setTimeZone(TimeZone.getTimeZone("GMT-8:00")); // 2020-3-8 23:48:6 System.out.println(formatter.format(date)); }
經常使用的格式化字符串: y:年 M:月 d:日 H:小時 m:分鐘 s:秒 S:毫秒 a:上午 / 下午 E:星期 z:時區code
Calendar
爲了加減日期,有了Calendar類orm
public static void main( String[] args ) { Calendar c = Calendar.getInstance(); //c.set(Calendar.DAY_OF_WEEK, 1); // 這裏設置後時間會自動變爲當週日的年月日 int y = c.get(Calendar.YEAR); // 不比加1900 int M = c.get(Calendar.MONDAY) + 1; // 仍然用0~11表示1~12月份 int d = c.get(Calendar.DAY_OF_MONTH); int H = c.get(Calendar.HOUR_OF_DAY); int m = c.get(Calendar.MINUTE); int s = c.get(Calendar.SECOND); int S = c.get(Calendar.MILLISECOND); int E = c.get(Calendar.DAY_OF_WEEK); System.out.println(y + "-" + M + "-" + d + " " + H + ":" + m + ":" + s + "." + S + " " + E); // 加減功能 SimpleDateFormat formatter = new SimpleDateFormat("y-M-d h:m:s"); System.out.println(formatter.format(c.getTime())); c.add(Calendar.DAY_OF_MONTH, 2); // 加2天 c.add(Calendar.MINUTE, -10); // 減10分鐘 System.out.println(formatter.format(c.getTime())); // 時區功能 c.clear(); c.set(2020, 2, 9, 16, 0, 0); //這表月份要減一,3月份就是2 c.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); System.out.println(c.getTime()); // Calendar獲取Date時會轉換成當地時區 }
LocalDateTime
public static void main( String[] args ) { LocalDateTime dt = LocalDateTime.now(); // ISO 8601格式 // yyyy-MM-dd'T'HH:mm:ss.SSS // 2020-03-09T16:23:47.667 System.out.println(dt); // 只須要Date或者Time LocalDate d = dt.toLocalDate(); LocalTime t = dt.toLocalTime(); // 設置 // 1.parse符合IOS 8601字符串 dt = LocalDateTime.parse("2020-03-09T16:23:47.667"); System.out.println(dt); // 2.of設置 dt = LocalDateTime.of(2019, 1, 1, 8, 10, 0); System.out.println(dt); // 3.單獨設置,對比Calendar類的Set LocalDateTime sdt = dt.withDayOfYear(6); System.out.println(sdt); // 計算,和Calendar有點不一樣,LocalDateti不是原址改變的,dt仍然是原來值 LocalDateTime dt2 = dt.minusDays(1).plusHours(3); System.out.println(dt + " " + dt2); }
ZonedDateTime
LocalDateTime老是當地時區,要對時區有操做能夠使用ZonedDateTimeip
public static void main( String[] args ) { // now獲取當前時間 ZonedDateTime date = ZonedDateTime.now(); ZonedDateTime dateEST = ZonedDateTime.now(ZoneId.of("America/New_York")); // 該時區此刻時間 System.out.println(date); // 2020-03-09T16:43:22.830+08:00[Asia/Shanghai] System.out.println(dateEST); // 2020-03-09T04:43:22.833-04:00[America/New_York] // LocalDateTime轉成ZonedDateTime,只是加了個時區信息,自己時間沒變 LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York")); System.out.println(ldt); // 2020-03-09T16:55:13.556 System.out.println(zdt); // 2020-03-09T16:55:13.556-04:00[America/New_York] // ZonedDateTime轉成LocalDateTime,簡單丟棄時區信息 ldt = zdt.toLocalDateTime(); System.out.println(ldt); // 時區轉換 ZonedDateTime zonedt = ZonedDateTime.of(2020, 1, 5, 10, 0, 0, 0, ZoneId.of("America/New_York")); ZonedDateTime zonedt2 = zonedt.withZoneSameInstant(ZoneId.of("Asia/Shanghai")); //2020-01-05T10:00-05:00[America/New_York] 2020-01-05T23:00+08:00[Asia/Shanghai] System.out.println(zonedt + " " + zonedt2); }
DateTimeFormatter
SimpleTimeFormat非線程安全,DateTimeFormatter線程安全字符串
public static void main( String[] args ) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("y-MM-dd H:m:s"); System.out.println(dtf.format(LocalDateTime.now())); //自定義格式初始化LocalDateTime LocalDateTime dt2 = LocalDateTime.parse("2020-01-01 10:14:43", dtf); System.out.println(dt2); }
Instant
Instant now = Instant.now(); System.out.println(now); System.out.println(now.getEpochSecond()); // 秒爲單位時間戳 System.out.println(now.toEpochMilli()); // 毫秒爲單位時間戳 // 建立ZonedDateTime now = Instant.ofEpochSecond(1583745633); ZonedDateTime zdt = now.atZone(ZoneId.systemDefault()); System.out.println(zdt);
新舊API轉換
// 舊轉新 Instant now = new Date().toInstant(); ZonedDateTime zdt1 = ZonedDateTime.ofInstant(now, ZoneId.systemDefault()); ZonedDateTime zdt2 = now.atZone(ZoneId.systemDefault()); System.out.println(zdt1 + " " + zdt2); // 新轉舊 Date d = new Date(zdt1.toEpochSecond()*1000); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(d));