Java日期轉換SimpleDateFormat格式大全

public class SimpleDateFormat extends DateFormat

SimpleDateFormat是一個特別敏感的方式格式化和分析數據的具體類。 它容許格式化 (date -> text)、語法分析 (text -> date)和標準化。 java

SimpleDateFormat容許覺得日期-時間格式化選擇任何用戶指定的方式啓動。 可是,但願用DateFormat中的getTimeInstance、getDateInstance或getDateTimeInstance建立一個日期-時間格式化程序。 每一個類方法返回一個以缺省格式化方式初始化的日期/時間格式化程序。 能夠根據須要用applyPattern方法修改格式化方式。

SimpleDateFormat函數的繼承關係:
java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.DateFormat
                   |
                   +----java.text.SimpleDateFormat

下面是個小例子:
import java.text.*;
import java.util.Date; app

/**
  SimpleDateFormat函數語法:
 
  G 年代標誌符
  y 年
  M 月
  d 日
  h 時 在上午或下午 (1~12)
  H 時 在一天中 (0~23)
  m 分
  s 秒
  S 毫秒
  E 星期
  D 一年中的第幾天
  F 一月中第幾個星期幾
  w 一年中第幾個星期
  W 一月中第幾個星期
  a 上午 / 下午 標記符
  k 時 在一天中 (1~24)
  K 時 在上午或下午 (0~11)
  z 時區
 */
public class FormatDateTime { 函數

    public static void main(String[] args) {
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
        SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價於now.toLocaleString()
        SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
        SimpleDateFormat myFmt4=new SimpleDateFormat(
                "一年中的第 D 天 一年中第w個星期 一月中第W個星期 在一天中k時 z時區");
        Date now=new Date();
        System.out.println(myFmt.format(now));
        System.out.println(myFmt1.format(now));
        System.out.println(myFmt2.format(now));
        System.out.println(myFmt3.format(now));
        System.out.println(myFmt4.format(now));
        System.out.println(now.toGMTString());
        System.out.println(now.toLocaleString());
        System.out.println(now.toString());
    }   
   
}

效果:
2004年12月16日 17時24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17時24分27秒 星期四
一年中的第 351 天 一年中第51個星期 一月中第3個星期 在一天中17時 CST時區
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

下面是個JavaBean:
public class FormatDateTime {
   
    public static String toLongDateString(Date dt){
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");       
        return myFmt.format(dt);
    }
   
    public static String toShortDateString(Date dt){
        SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時mm分");       
        return myFmt.format(dt);
    }   
   
    public static String toLongTimeString(Date dt){
        SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");       
        return myFmt.format(dt);
    }
    public static String toShortTimeString(Date dt){
        SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");       
        return myFmt.format(dt);
    }
   
    public static void main(String[] args) { 測試

        Date now=new Date(); spa

        System.out.println(FormatDateTime.toLongDateString(now));
        System.out.println(FormatDateTime.toShortDateString(now));
        System.out.println(FormatDateTime.toLongTimeString(now));
        System.out.println(FormatDateTime.toShortTimeString(now));
    }   
   
}
調用的main 測試結果:
2004年12月16日 17時38分26秒 星期四
04年12月16日 17時38分
17 38 26 0965
04/12/16 17:38 orm

 

24小時制時間顯示: blog


publicclass Datetime{

    public staticvoid main(String args[]){
         java.util.Datecurrent=newjava.util.Date();
           java.text.SimpleDateFormat sdf=newjava.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String c=sdf.format(current);
           System.out.println(c);
    }
}
繼承

12小時制時間顯示: ci


public class Datetime {

    public staticvoid main(String args[]){
         java.util.Datecurrent=newjava.util.Date();
           java.text.SimpleDateFormat sdf=newjava.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
           String c=sdf.format(current);
           System.out.println(c);
    }
}
get

二者區別:yyyy-MM-dd HH:mm:ss ;  yyyy-MM-dd hh:mm:ss

以下:


字母 日期或時間元素 表示 示例
G Era 標誌符 Text AD
y Year 1996;96
M 年中的月份 Month July;Jul;07
w 年中的週數 Number 27
W 月份中的週數 Number 2
D 年中的天數 Number 189
d 月份中的天數 Number 10
F 月份中的星期 Number 2
E 星期中的天數 Text Tuesday;Tue
a Am/pm 標記 Text PM
H 一天中的小時數(0-23) Number 0
k 一天中的小時數(1-24) Number 24
K am/pm 中的小時數(0-11) Number 0
h am/pm 中的小時數(1-12) Number 12
m 小時中的分鐘數 Number 30
s 分鐘中的秒數 Number 55
S 毫秒數 Number 978
z 時區 General time zone Pacific Standard Time;PST;GMT-08:00
Z 時區 RFC 822 time zone -0800
相關文章
相關標籤/搜索