欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java中的Calendar日历类

程序员文章站 2024-03-17 11:47:04
...
  • 示例代码
import java.util.Calendar;//日历类

public class CalendarDemo {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();//创建日历对象
        System.out.println(calendar);
        System.out.println(calendar.getTime());//日历对象转为日期对象
        //获取日历对象的信息 get方法
        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.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));

        //设置日历对象中的值
        calendar.set(8888,8,8,8,8,8);
        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.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
    }
}
  • 结果输出
java.util.GregorianCalendar[time=1607417793872,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=343,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=56,SECOND=33,MILLISECOND=872,ZONE_OFFSET=28800000,DST_OFFSET=0]
Tue Dec 08 16:56:33 CST 2020
2020
11
8
16
56
33
8888
8
8
8
8
8

上一篇: ThreadLocal

下一篇: 文件读写(java)