Calendar类

2021/7/15 23:17:05

本文主要是介绍Calendar类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Demo01 {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        System.out.println("calendar:::" + calendar);//
        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_MONTH));//日
        System.out.println("时:" + calendar.get(Calendar.HOUR));//时
        System.out.println("分:" + calendar.get(Calendar.MINUTE));//分

        calendar.set(Calendar.YEAR,2020);//设置年
        System.out.println("年:" + calendar.get(Calendar.YEAR));//年
        System.out.println("当月的最大天数:"+calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//当月的最大天数


        //Date和Calendar之间的转换
        Date date = new Date();
        Calendar calendar1= new GregorianCalendar();
        calendar1.setTime(date);//Date类转为日历类
        System.out.println("Date类转为日历类:"+date);
        Date time = calendar1.getTime();//日历类转为Date类
        System.out.println("日历类转为Date类:"+time);
    }
}

//运行结果:

calendar:::java.util.GregorianCalendar[time=1626356605671,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=2021,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=15,DAY_OF_YEAR=196,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=43,SECOND=25,MILLISECOND=671,ZONE_OFFSET=28800000,DST_OFFSET=0]
年:2021
月:6
日:15
日:15
时:9
分:43
年:2020
当月的最大天数:31
Date类转为日历类:Thu Jul 15 21:43:25 CST 2021
日历类转为Date类:Thu Jul 15 21:43:25 CST 2021

Process finished with exit code 0


这篇关于Calendar类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程