import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import static java.lang.System.*;
/**
* Created by leo on 17-7-11.
* GregorianCalendar默认以当前日期构建一个GregorianCalendar对象
* 可以获得年月日,时分秒等信息
* 更多操作参考java官方API
*
*
下面的内容有如下格式:Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year (context sensitive) Month July; Jul; 07
L Month in year (standalone form) Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
*/
public class GregorianCalendarNote {
public static void main(String[] args){
// GregorianCalendar exetends Calendar
Calendar cal = new GregorianCalendar();
out.println(cal);
out.println(cal.getFirstDayOfWeek());
out.println(cal.getTimeZone());
out.println(cal.get(Calendar.YEAR));
out.println(cal.get(Calendar.MONTH + 1));
out.println(cal.get(Calendar.DAY_OF_MONTH));
out.println(cal.get(Calendar.HOUR));
out.println(cal.get(Calendar.MINUTE));
out.println(cal.get(Calendar.SECOND));
out.println(cal.get(Calendar.DAY_OF_YEAR));
out.println(cal.getTime());
cal.set(2012,12,2);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
SimpleDateFormat format4 = new SimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now = new Date();
out.println(format1.format(now));
out.println(format2.format(now));
out.println(format3.format(now));
out.println(now.toLocaleString());
out.println(format4.format(now));
}
}
/* 结果如下
java.util.GregorianCalendar[time=1499737898037,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=3,DAY_OF_MONTH=11,DAY_OF_YEAR=192,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=51,SECOND=38,MILLISECOND=37,ZONE_OFFSET=28800000,DST_OFFSET=0]
1
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
2017
28
11
9
51
38
192
Tue Jul 11 09:51:38 CST 2017
2017:07:11 09:51:38
2017年07月11日 09时51分38秒 Tue
2017-07-11 09:51:38
Jul 11, 2017 9:51:38 AM
一年中的第 192 天 一年中第28个星期 一月中第3个星期 在一天中9时 CST时区
*/
Java基础之SimpleDateFormat和GregorianCalendar的使用
最新推荐文章于 2022-03-10 09:41:39 发布