Java Calendar类的使用

Java Calendar 类用来处理日历。

基本使用

一目了然。

Calendar calendar = Calendar.getInstance();
System.out.println("calendar:" + calendar);
System.out.println("日历类型:" + calendar.getCalendarType());
System.out.println("日期:" + calendar.getTime());
System.out.println("年:" + calendar.get(Calendar.YEAR));
System.out.println("月:" + calendar.get(Calendar.MONTH));
System.out.println("日:" + calendar.get(Calendar.DATE));
System.out.println("这一年中的第几天:" + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("这一月中的第几天:" + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("这一周中的第几天:" + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("时:" + calendar.get(Calendar.HOUR));
System.out.println("分:" + calendar.get(Calendar.MINUTE));
System.out.println("秒:" + calendar.get(Calendar.SECOND));
System.out.println("时区:" + calendar.getTimeZone());
System.out.println("时区ID:" + calendar.getTimeZone().getID());
System.out.println("时区名称:" + calendar.getTimeZone().getDisplayName());

运行结果,

calendar:java.util.GregorianCalendar[time=1695450278644,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=2023,MONTH=8,WEEK_OF_YEAR=38,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=266,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=24,SECOND=38,MILLISECOND=644,ZONE_OFFSET=28800000,DST_OFFSET=0]
日历类型:gregory
日期:Sat Sep 23 14:24:38 CST 2023
年:2023
月:8
日:23
这一年中的第几天:266
这一月中的第几天:23
这一周中的第几天:7
时:2
分:24
秒:38
时区:sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
时区ID:Asia/Shanghai
时区名称:中国标准时间

注意 calendar.get(Calendar.MONTH) ,是从 0 开始的,使用时需要 +1 。

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
See Also:
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER, UNDECIMBER

public final static int MONTH = 2;

指定日期

Calendar calendar1 = Calendar.getInstance();
calendar1.set(2023, Calendar.SEPTEMBER, 30);//2023/9/30
System.out.println("calendar1.getTime:" + calendar1.getTime());

运行结果,

calendar1.getTime:Sat Sep 30 14:17:34 CST 2023

某个日期的前后几天

配合 SimpleDateFormat ,可以让输出直观一点。

SimpleDateFormat sf = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2023, Calendar.SEPTEMBER, 30);//2023/9/30
System.out.println("calendar1.getTime:" + calendar1.getTime());
System.out.println("date:" + sf.format(calendar1.getTime()));
calendar1.add(Calendar.DAY_OF_MONTH, -1);/、前一天
System.out.println("after add -1 day, date:" + sf.format(calendar1.getTime()));

calendar1.roll(Calendar.DAY_OF_MONTH, 2);//后两天
System.out.println("after roll 2 day , date:" + sf.format(calendar1.getTime()));

运行结果,

calendar1.getTime:Sat Sep 30 14:17:34 CST 2023
date:2023/9/30 14:17:34
after add -1 day, date:2023/9/29 14:17:34
after roll 2 day , date:2023/9/01 14:17:34

你可能感兴趣的:(Java,java,Calendar)