Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Calendar class in java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the Calendar class in java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

* * Overview of Calendar class

Calendar is a calendar class, which encapsulates all possible time information into static member variables, which is easy to obtain. The common methods are as follows:

/ / add or subtract a specified amount of time from a given calendar field according to the rules of the calendar. Abstract void add (int field, int amount) / / determines whether the time represented by the Calendar is after the time represented by the specified Object, and returns the judgment result. Boolean after (Object when) / / determines whether the time represented by this Calendar is before the time represented by the specified Object, and returns the judgment result. Boolean before (Object when) / / given the time value of this Calendar, returns the maximum value that the specified calendar field may have. Int getActualMaximum (int field) / / gives the time value of this Calendar and returns the minimum value that the specified calendar field may have. Int getActualMinimum (int field) / / gets the first day of the week; for example, in the United States, this day is SUNDAY, while in France, it is MONDAY. Int getFirstDayOfWeek () / / returns the highest minimum value of a given calendar field for this Calendar instance. Abstract int getGreatestMinimum (int field) / / get a calendar using the default time zone and locale. Static Calendar getInstance () / / gets a calendar using the default time zone and the specified locale. Static Calendar getInstance (Locale aLocale) / / gets a calendar using the specified time zone and default locale. Static Calendar getInstance (TimeZone zone) / / gets a calendar using the specified time zone and locale. Static Calendar getInstance (TimeZone zone, Locale aLocale) / / returns the lowest maximum value of a given calendar field for this Calendar instance. Abstract int getLeastMaximum (int field) / / returns the maximum value of the calendar field given by this Calendar instance. Abstract int getMaximum (int field) / / gets the minimum number of days required for the first week of the year, for example, if the first week is defined to contain the first day of the first month of the year, this method returns 1. Int getMinimalDaysInFirstWeek () / / sets the first day of the week; for example, in the United States, it is SUNDAY, while in France, it is MONDAY. Void setFirstDayOfWeek (int value) / / sets the minimum number of days required for the first week of the year, for example, if you define the first day of the first week that contains the first month of the year, call this method with a value of 1. Void setMinimalDaysInFirstWeek (int value) / / sets the time of this Calendar using the given Date. Void setTime (Date date) / / sets the current time value of this Calendar with the given long value. Void setTimeInMillis (long millis) / / returns a string representation of this calendar. String toString ()

Note: there are many properties of the Calendar class, such as:

YEAR year

MONTH month (Note: starting from 0, the maximum 11 position 0 represents January and 11 represents December)

DATE days

When HOUR

MINUTE score

SECOND second

* * two. The usage cases are listed as follows:

Calendar calendar=Calendar.getInstance (); / / obtain the reference to Calendar through the getInstance method Calendar.setTime (new Date ()); System.out.println ("time now is:" + new Date ()); String year=String.valueOf (calendar.get (Calendar.YEAR)); / / which year is String month=String.valueOf (calendar.get (Calendar.MONTH) + 1); / / what month is String day=String.valueOf (calendar.get (Calendar.DAY_OF_MONTH)) now? / / the day of the month String week=String.valueOf (calendar.get (Calendar.DAY_OF_WEEK)-1); / / the day of the week System.out.println ("present time is: + year+" year + month+ "month" + day+ "day, week" + week); long year2016=calendar.getTimeInMillis (); calendar.set (1989 Magazine 26) / / the difference between here and the real month is 1 / / set time (year,month,day) long year1989=calendar.getTimeInMillis (); long days= (year2016-year1989) / (10000,60,60,24,000,60,60,24); System.out.println ("today and October 26th, 1989" + days+ "days," that is to say, I have lived a happy life on this beautiful planet "+ days+"). ; System.out.println (calendar.get (Calendar.DAY_OF_YEAR)); / / the day after getting the setting is the day of the year System.out.println (calendar.get (Calendar.DAY_OF_MONTH)); / / the day after getting the setting is the day of the month System.out.println (calendar.get (Calendar.DAY_OF_WEEK)) / / the day after getting the setting is the first day of the week, which is all the contents of the article "how to use Calendar classes in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report