Java 日期格式模式

2018-02-12 21:04 更新

Java格式 - Java日期格式模式


SimpleDateFormat日期和時(shí)間格式由日期和時(shí)間模式字符串指定。

在格式字符串中,從“A"到“Z"和從“a"到“z"被視為表示日期或時(shí)間字符串的組成部分的格式字母。

文本可以使用單引號(hào)(“)引用,以避免解釋。“"表示單引號(hào)。 所有其他字符不解釋并且將被簡(jiǎn)單地復(fù)制到輸出字符串中。

模式

定義了以下模式字母:

字母日期或時(shí)間組件例子
G時(shí)代指示符AD
y2014; 14
Y周年2014; 14
M年份中的月份(上下文相關(guān))July; Jul; 07
L每年的月份(獨(dú)立形式)July; Jul; 07
w每年的一周27
W每月一周2
D每年的一天189
d月份日10
F每月的星期幾2
E周的名稱Tuesday; Tue
u周數(shù)(1 =星期一,...,7 =星期日)1
aAm/pm標(biāo)記PM
H時(shí)段(1-23)0
k時(shí)段(1-24)24
K上午/下午時(shí)間(0-11)0
h上午/下午時(shí)間(1-12)12
m分鐘,小時(shí)30
s第二分鐘55
S毫秒978
z時(shí)區(qū)Pacific Standard Time; PST; GMT-08:00
Z時(shí)區(qū)-0800
X時(shí)區(qū)-08; -0800; -08:00

例子

下表有一些示例格式字符串及其結(jié)果。

日期和時(shí)間模式結(jié)果
"yyyy.MM.dd G "at" HH:mm:ss z"2014.08.04 AD at 12:08:56 PDT
"EEE, MMM d, ""yy"Wed, Jul 4, "01
"h:mm a"11:08 PM
"hh "o""clock" a, zzzz"12 o"clock PM, Pacific Daylight Time
"K:mm a, z"0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"02014.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"020704120856-0700
"yyyy-MM-dd"T"HH:mm:ss.SSSZ"2014-07-04T12:08:56.235-0700
"yyyy-MM-dd"T"HH:mm:ss.SSSXXX"2014-07-04T12:08:56.235-07:00
"YYYY-"W"ww-u"2014-W27-3

例2

我們可以將字面量嵌入格式化的日期。

我們需要將它們放在單引號(hào)中以將它們作為文字。

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc  = new GregorianCalendar(2010, Calendar.SEPTEMBER,9);
    Date  birthDate = gc.getTime();

    String pattern = ""I was born on the day" dd "of the month "MMMM "in" yyyy";

    SimpleDateFormat simpleFormatter  = new SimpleDateFormat(pattern);
    System.out.println(simpleFormatter.format(birthDate));
  }

}

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

解析字符串到日期

我們可以使用parse()方法將String值轉(zhuǎn)換為日期時(shí)間值 SimpleDateFormat 類。

parse()方法的簽名如下:

public Date parse(String text, ParsePosition startPos)

parse()方法接受兩個(gè)參數(shù)。text是要解析的字符串,startPos設(shè)置文本中字符從您要開始解析的位置開始的位置。

例3

以下代碼顯示如何將字符串解析為日期值。

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    String text = "09/19/2014";
    // Create a pattern for the date text "09/19/2014"
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);
    // a ParsePosition object with value zero
    ParsePosition startPos = new ParsePosition(0);
    // Parse the text
    Date parsedDate = simpleFormatter.parse(text, startPos);
    
    System.out.println(parsedDate);
  }

}

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

例4

以下代碼解析單個(gè)字符串中的兩個(gè)日期值。

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    String text = "ab01/01/1999cd12/31/2000ef";
    String pattern = "MM/dd/yyyy";

    SimpleDateFormat simpleFormatter  = new SimpleDateFormat(pattern);

    // Set  the   start index   at 2
    ParsePosition startPos  = new ParsePosition(2);

    // Parse the   text to get   the   first date (January 1,  1999) 
    Date  firstDate = simpleFormatter.parse(text, startPos);
    System.out.println(firstDate);

   //Now, startPos has  its index set after the last  character of the first date parsed.
   
    int  currentIndex = startPos.getIndex();
    System.out.println(currentIndex);
    // To set its index   to the   next   date increment its index   by  2. 

    int  nextIndex = currentIndex + 2;
    startPos.setIndex  (nextIndex);

    // Parse the   text to get   the   second  date (December  31,   2000) 
    Date  secondDate = simpleFormatter.parse(text, startPos);
    System.out.println(secondDate);

  }

}

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

例5

以下代碼顯示如何解析時(shí)間戳以獲取時(shí)間零件

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    String input = "2014-05-04 09:10:40.321";

    // Prepare the pattern
    String pattern = "yyyy-MM-dd HH:mm:ss.SSS";

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    // Parse the text into a Date object
    Date dt = sdf.parse(input, new ParsePosition(0));
    System.out.println(dt);

    // Get the Calendar instance
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);

    // Print time parts
    System.out.println("Hour:" + cal.get(Calendar.HOUR));
    System.out.println("Minute:" + cal.get(Calendar.MINUTE));
    System.out.println("Second:" + cal.get(Calendar.SECOND));
    System.out.println("Millisecond:" + cal.get(Calendar.MILLISECOND));

  }
}

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

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)