yyyy-MM-dd HH:mm:ss
y 不包含纪元的年份。不具有前导零。
yy 不包含纪元的年份。具有前导零。
yyyy 包括纪元的四位数的年份。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
使用SimpleDateFormat输出时间和日期字符串
@Test
public void test8978() {
Date now = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2014-09-15 12:29:59
System.out.println(f.format(now));
}
yyyy-MM-dd HH:mm:ss只要是其子串就可以输出相应的时间和日期字符串
@Test
public void test8978() {
Date now = new Date();
SimpleDateFormat f = new SimpleDateFormat("HHmmss");
System.out.println(f.format(now));//124457
SimpleDateFormat f1 = new SimpleDateFormat("HH:mm:ss");
System.out.println(f1.format(now)); //12:44:57
SimpleDateFormat f2 = new SimpleDateFormat("dd HH:mm:ss");
System.out.println(f2.format(now)); //15 12:44:57
}
DateFormat输出时间和日期字符串
@Test
public void test9089() {
Date now = new Date();
//输出日期字符串
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println(df1.format(now)); //14-9-15
DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM);
System.out.println(df2.format(now)); //2014-9-15
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
System.out.println(df3.format(now)); //2014年9月15日
DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL);
System.out.println(df4.format(now)); //2014年9月15日 星期一
DateFormat df5 = DateFormat.getDateInstance(DateFormat.DEFAULT);
System.out.println(df5.format(now)); //2014-9-15
/**
* DateFormat.getTimeInstance
*/
//输出时间字符串
DateFormat df6 = DateFormat.getTimeInstance(DateFormat.SHORT);
System.out.println(df6.format(now)); //下午12:22
DateFormat df7 = DateFormat.getTimeInstance(DateFormat.MEDIUM);
System.out.println(df7.format(now)); //12:22:41
DateFormat df8 = DateFormat.getTimeInstance(DateFormat.LONG);
System.out.println(df8.format(now)); //下午12时22分41秒
DateFormat df9 = DateFormat.getTimeInstance(DateFormat.FULL);
System.out.println(df9.format(now)); //下午12时22分41秒 CST
}
DateFormat类中表示日期和时间的输出样式的常量:
/**
* Constant for full style pattern.
*/
public static final int FULL = 0;
/**
* Constant for long style pattern.
*/
public static final int LONG = 1;
/**
* Constant for medium style pattern.
*/
public static final int MEDIUM = 2;
/**
* Constant for short style pattern.
*/
public static final int SHORT = 3;
/**
* Constant for default style pattern. Its value is MEDIUM.
*/
public static final int DEFAULT = MEDIUM;
=====END=====