210 calender


210 calender

Calender类概述

file:///C:/Users/13833123813/Desktop/jdk-17_doc-all/docs/api/java.base/java/util/Calendar.html

Module java.base

Package java.util

class Calendar

java.lang.Object

java.util.Calendar

1

All Implemented Interfaces:

Serializable, Cloneable, Comparable<Calendar>

Direct Known Subclasses:

扫描二维码关注公众号,回复: 13340209 查看本文章

GregorianCalendar

public abstract class Calendar

extends Object

implements Serializable, Cloneable, Comparable<Calendar>

所有实现的接口:

可序列化、可克隆、可比较<日历>

直接已知子类:

GregorianCalendar

public abstract class Calendar

扩展对象

实现可序列化、可克隆、可比较的<Calendar>

2

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields字段 such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

An instant瞬间 in time can be represented代表 by a millisecond value that is an offset抵消 from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

Calendar类是一个抽象类,它提供了一些方法,用于在特定的时间瞬间和一组日历字段(如年、月、月中日、小时等)之间进行转换,并用于操作日历字段(如获取下周的日期)

时间上的一个瞬间可以用毫秒值表示,毫秒值是从1970年1月1日00:00:00.000 GMT(格里高利)开始的历元偏移量。

3

The class also provides additional fields and methods for implementing a concrete calendar system outside the package.

Those fields and methods are defined as protected.

该类还提供了用于在包外部实现具体日历系统的附加字段和方法。

这些字段和方法被定义为受保护。

4

Like other locale-sensitive敏感 classes, Calendar provides a class method, getInstance, for getting a generally useful常用的 object of this type.

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:

     Calendar rightNow = Calendar.getInstance();

与其他对区域设置敏感的类一样,Calendar提供了一个类方法getInstance,用于获取这种类型的常用对象。

Calendar的getInstance方法返回一个日历对象,该对象的日历字段已使用当前日期和时间初始化:

Calendar rightNow=Calendar.getInstance();

5

A Calendar object can produce all the calendar field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).

Calendar defines the range of values returned by certain calendar fields, as well as their meaning.

For example, the first month of the calendar system has value MONTH == JANUARY for all calendars.

Other values are defined by the concrete subclass, such as ERA. See individual field documentation and subclass documentation for details.

Calendar对象可以生成实现特定语言和日历样式(例如,日文公历、日文繁体)的日期时间格式所需的所有日历字段值。

Calendar定义某些日历字段返回的值的范围及其含义。

例如,对于所有日历,日历系统的第一个月的值month==一月。其他值由具体的子类定义,例如ERA。

有关详细信息,请参阅单个字段文档和子类文档。

--------------------------------------------------------------

(module)myCalender

(package)it01e210

class)CalenderDemo

--------------------------------------------------------------

package it01e210;

import java.util.Calendar;

//Calendar类是一个抽象类,它提供了一些方法,

// 用于在特定的时间瞬间和一组日历字段(如年、月、月中日、小时等)之间进行转换,并用于操作日历字段(如获取下周的日期)。

//Calendar提供了一个类方法getInstance,用于获取这种类型的常用对象。

//Calender rightNow = Calender.getInstance();

public class CalenderDemo {

    public static void main(String[] args) {

        //get object by getInstance-method

        //write Ca and then choose one from the following choices, so you import the package easily.

        Calendar c = Calendar.getInstance();//ctrl b"getInstance" and we know that this method implements by polymorphism

        System.out.println(c);

//OUTPUT:java.util.GregorianCalendar[time=1632323465797,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=2021,MONTH=8,WEEK_OF_YEAR=39,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=265,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=11,SECOND=5,MILLISECOND=797,ZONE_OFFSET=28800000,DST_OFFSET=0]

        //the OUTPUT tells that the month start from 0 instead of 1

        //use get-method to get value of year,month,date

        int year = c.get(Calendar.YEAR);//Calender.YEAR gives the int-type-parameter

        int month = c.get(Calendar.MONTH)+1;//ATTENTION:add 1 here

        int date = c.get(Calendar.DATE);

        System.out.println(year+"年"+month+"月"+date+"日");//OUTPUT:2021年9月22日

    }

}

猜你喜欢

转载自blog.csdn.net/m0_63673788/article/details/121508327