java日期格式大全 format SimpleDateFormat

来源:互联网 发布:战翼cg 知乎 编辑:程序博客网 时间:2024/05/21 07:15

 http://eternal1025.iteye.com/blog/344360

http://www.freejsp.net/Spring/13325.htm

1.日期型 转换为 String
DateFormat   f   =   new   SimpleDateFormat( "yyyy-MM-dd   hh:MM ");
String t=f.format(date);

2.String ->Date
Date date=Util.strToDateTimeYMD(t);


模板定义如下:
y                 年号,如   1996
M                 月份,如   July   或者   07
d                 月中第几天,如   12
H                 小时(24制),如   0、17
m                 分钟,如   32
s                 钞钟,如55
S                 微钞,如978
E                 星期几,如   Tuesday
D                 一年中的第几天,如   189
w                 week   in   year                         (Number)                         27
W                 week   in   month                     (Number)                         2
a                 am/pm   marker                         (Text)                             PM
k                 hour   in   day   (1~24)             (Number)                         24
K                 hour   in   am/pm   (0~11)         (Number)                         0
z                 time   zone                             (Text)                             Pacific   Standard   Time
'                 escape   for   text                 (Delimiter)
' '             single   quote                         (Literal)

STRING格式
dow mon dd hh:mm:ss zzz yyyy其中:
dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。 
mon 是月份 (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)。 
dd 是一月中的某一天(01 至 31),显示为两位十进制数。 
hh 是一天中的小时(00 至 23),显示为两位十进制数。 
mm 是小时中的分钟(00 至 59),显示为两位十进制数。 
ss 是分钟中的秒数(00 至 61),显示为两位十进制数。 
zzz 是时区(并可以反映夏令时)。标准时区缩写包括方法 parse 识别的时区缩写。如果不提供时区信息,则 zzz 为空,即根本不包括任何字符。 
yyyy 是年份,显示为 4 位十进制数。
http://stackoverflow.com/questions/4993132/getting-java-lang-illegalargumentexception-illegal-pattern-character-o-while
Actually from java docs I got that format for util's Date.toString() function
String java.util.Date.toString()

Converts this Date object to a String of the form:

 dow mon dd hh:mm:ss zzz yyyy
where:
  • dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
  • mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
  • dd is the day of the month (01 through 31), as two decimal digits.
  • hh is the hour of the day (00 through 23), as two decimal digits.
  • mm is the minute within the hour (00 through 59), as two decimal digits.
  • ss is the second within the minute (00 through 61, as two decimal digits.
  • zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the methodparse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.
  • yyyy is the year, as four decimal digits.
Overrides: toString() in Object
Returns:
a string representation of this date.
See Also:
java.util.Date.toLocaleString()
java.util.Date.toGMTString()

==========

http://java.chinaitlab.com/base/747297.html

24小时制时间显示:
 

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小时制时间显示:


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);
    }
}

两者区别:yyyy-MM-dd HH:mm:ss ;  yyyy-MM-dd hh:mm:ss

如下:

字母日期或时间元素表示示例GEra 标志符TextADyYear1996; 96M年中的月份MonthJuly; Jul;07w年中的周数Number27W月份中的周数Number2D年中的天数Number189d月份中的天数Number10F月份中的星期Number2E星期中的天数TextTuesday; TueaAm/pm 标记TextPMH一天中的小时数(0-23)Number0k一天中的小时数(1-24)Number24Kam/pm 中的小时数(0-11)Number0ham/pm 中的小时数(1-12)Number12m小时中的分钟数Number30s分钟中的秒数Number55S毫秒数Number978z时区General time zonePacific Standard Time; PST; GMT-08:00Z时区RFC 822 time zone-0800

原创粉丝点击