Java 預(yù)定義日期時(shí)間格式

2018-03-12 15:09 更新

Java日期時(shí)間 - Java預(yù)定義日期時(shí)間格式


預(yù)定義格式化程序在DateTimeFormatter 類中定義為常量。

  • BASIC_ISO_DATE
    格式并解析日期,而不使用兩個(gè)日期組件之間的分隔符。
    例如:20140209
    20140221-0600
  • ISO_DATE
    格式并使用ISO分隔符解析日期,時(shí)間和數(shù)據(jù)時(shí)間。
    例如:2014-02-09
  • ISO_TIME
    格式并使用ISO分隔符解析日期,時(shí)間和數(shù)據(jù)時(shí)間。
    例如:15:21:32.927
    15:21:32.123-06:00
  • ISO_DATE_TIME
    格式并使用ISO分隔符解析日期,時(shí)間和數(shù)據(jù)時(shí)間。
    例如:2014-02-09-06:00
    2014-02-09T15:20:07.123-06:00
    2014-02-09T15:20:07.123-06:00[America/Chicago]
  • ISO_INSTANT
    格式并解析表示時(shí)間的即時(shí)或日期時(shí)間對(duì)象,例如UTC格式的ZonedDateTime。
    例如:2014-02-09T21:23:56.870Z
  • ISO_LOCAL_DATE
    格式或解析日期沒(méi)有偏移。
    例如:2014-02-09
  • ISO_LOCAL_TIME
    格式或解析時(shí)間沒(méi)有偏移。
    例如:15:30:12.123
  • ISO_LOCAL_DATE_TIME
    格式或解析無(wú)偏移的數(shù)據(jù)時(shí)間。
    例如:2014-02-09T15:29:11.214
  • ISO_OFFSET_DATE
    格式并使用ISO格式解析具有偏移量的日期。
    例如:2014-02-09-06:00
  • ISO_OFFSET_TIME
    格式并使用ISO格式解析具有偏移量的時(shí)間。
    例如:15:12:29.123-06:00
  • ISO_OFFSET_DATE_TIME
    格式并使用ISO格式解析具有偏移量的數(shù)據(jù)時(shí)間。
    例如:2014-02-09T15:33:07.07-06:0
  • ISO_ZONED_DATE_TIME
    格式并使用區(qū)域ID解析日期時(shí)間(如果可用)。
    例如:2014-02-09T15:45:49.123-06:00
    2014-02-09T15:45:12.112-06:00[America/Chicago]
  • ISO_ORDINAL_DATE
    格式并解析具有年和年的日期。
    例如:2014-009
  • ISO_WEEK_DATE
    格式并解析基于周的日期。 “2014-W02-4”表示2014年第二周的第四天。
    例如:2014-W02-4
    2014-W02-4-06:00
  • RFC_1123_DATE_TIME
    格式并使用RFC1123規(guī)范解析電子郵件的數(shù)據(jù)時(shí)間。
    例如:Thu, 9 Jan 2014 15:12:12 -060

例子

我們可以使用DateTimeFormatter類中的以下方法來(lái)格式化日期時(shí)間值。

String format(TemporalAccessor temporal)
void formatTo(TemporalAccessor temporal, Appendable  appendable)

以下代碼顯示如何使用ISO_DATE格式化程序格式化LocalDate,OffsetDateTime和ZonedDateTime。

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main{
  public static void main(String[] argv){
    String ldStr = DateTimeFormatter.ISO_DATE.format(LocalDate.now());
    System.out.println(ldStr);
    String odtStr = DateTimeFormatter.ISO_DATE.format(OffsetDateTime.now());
    System.out.println(odtStr);
    String zdtStr = DateTimeFormatter.ISO_DATE.format(ZonedDateTime.now());
    System.out.println(zdtStr);
  }
}

上面的代碼生成以下結(jié)果。


例2

我們還可以使用格式()從日期時(shí)間類格式化日期時(shí)間對(duì)象。

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
  public static void main(String[] argv) {
    LocalDate ld = LocalDate.now();
    String ldStr = ld.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Local  Date: " + ldStr);

    OffsetDateTime odt = OffsetDateTime.now();
    String odtStr = odt.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Offset  Datetime: " + odtStr);

    ZonedDateTime zdt = ZonedDateTime.now();
    String zdtStr = zdt.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Zoned  Datetime: " + zdtStr);
  }
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)