java 日历和GregorianCalendar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 日历和GregorianCalendar相关的知识,希望对你有一定的参考价值。

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

public class Main {

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance(); //Get the current date and time

        //Print out a constant from the calendar
        System.out.println("Year: " + calendar.get(Calendar.YEAR));
        //Print out the full date
        System.out.println("Date: " + (calendar.get(Calendar.MONTH) + 1) + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR));
        //Time
        System.out.println("Time: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));

        //If you want to show the actual month in english
        String monnths[] = {
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        };
        System.out.println("Month: " + monnths[calendar.get(Calendar.MONTH)]);

        //Setting the values of the Calendar
        calendar.set(2019, 0, 16);
        calendar.set(Calendar.SECOND, 25);
        calendar.set(Calendar.HOUR, 11);
        System.out.println("Updated Date and Time");
        System.out.println("Date: " + (calendar.get(Calendar.MONTH) + 1) + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR));
        System.out.println("Time: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));

        System.out.println(calendar.getTime()); //Another simple way to get time or date object

        //The Gregorian Calendar
        GregorianCalendar gcal = new GregorianCalendar(); //Use your current time
        GregorianCalendar gcal2 = new GregorianCalendar(2019, 2, 14);
        //Since gregorian calendar is concrete, you can instantiate ^

    }
}

以上是关于java 日历和GregorianCalendar的主要内容,如果未能解决你的问题,请参考以下文章

调用java.util包中的Date和GregorianCalendar类来制作万年历 能够结合java.util包

Java:GregorianCalendar 的最大值和最小值是啥/在哪里?

JPA 和公历日历

日历 System.Globalization.GregorianCalendar 不支持字符串

java_日历类

如何格式化 GregorianCalendar