In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Java Calendar calendar class principle and usage", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Java Calendar calendar class principle and use method" bar!
Java.util.Calendar is a calendar class that appears after Date and replaces many of Date's methods. This class encapsulates all the time information that may be used as static member variables, which is easy to obtain. The calendar class is easy to get each time attribute. The Calendar class cannot be used directly to create objects, and there is a static method getInstance () that returns a subclass object of the Calendar class. Many member constants are provided in the Calendar class, representing a given calendar field: as shown below
one
Acquisition mode
Calendar is an abstract class. Due to language sensitivity, Calendar class is not created directly when creating objects, but is created by static methods and returns subclass objects, as shown below:
Calendar static method
Public static Calendar getInstance (): get a calendar using the default time zone and locale
/ / get the subclass object Calendar cal = Calendar.getInstance () of the Calendar class
Common methods
According to the API documentation of the Calendar class, the common methods are:
Public int get (int field): returns the value of a given calendar field.
Package demo04;import java.util.Calendar;public class Demo01 {public static void main (String [] args) {/ / is created by a static method and returns the Calendar subclass object Calendar instance = Calendar.getInstance (); / * public int get (int field): returns the value of a given calendar field. Parameter: pass the specified calendar field (YEAR,MONTH...) Return value: the specific value represented by the calendar field * / System.out.print ("now" + instance.get (Calendar.YEAR) + "year"); / / in the Calendar class, the month is represented by 0-11 for January-December. System.out.print (instance.get (Calendar.MONTH) + month); System.out.println (instance.get (Calendar.DAY_OF_MONTH) + day);}}
The result after the code execution
two
Public void set (int field, int value): sets the given calendar field to the given value.
Package demo04;import java.util.Calendar;public class Demo02 {/ * public void set (int field, int value): sets the given calendar field to the given value. Parameter: int field: pass the specified calendar field (YEAR,MONTH...) Int value: set the value for the specified field * / public static void main (String [] args) {/ / use the getInstance method to get the Calendar object Calendar c = Calendar.getInstance (); / / set the year to 9999 c.set (Calendar.YEAR, 9999); / / set the month to September c.set (Calendar.MONTH, 9); / / set the 9th c.set (Calendar.DATE, 9) / / get the set date System.out.println ("current date is" + c.get (Calendar.YEAR) + "year" + c.get (Calendar.MONTH) + "month" + c.get (Calendar.DAY_OF_MONTH) + "day"); / / set the year, month and day at the same time, you can use set's overload method c.set (8888, 8, 8) System.out.println ("current date is" + c.get (Calendar.YEAR) + "year" + c.get (Calendar.MONTH) + "month" + c.get (Calendar.DAY_OF_MONTH) + "Day");}}
The result after the code execution
three
Public abstract void add (int field, int amount): adds or subtracts a specified amount of time from a given calendar field according to the rules of the calendar.
Package demo04;import java.util.Calendar;public class Demo03 {/ * public abstract void add (int field, int amount): adds or subtracts a specified amount of time from a given calendar field according to the rules of the calendar. Add / decrease the specified field by the specified value parameter: int field: pass the specified calendar field (YEAR,MONTH...) Int amount: increase / decrease specified positive values: increase negative numbers: decrease * / public static void main (String [] args) {/ / use the getInstance method to get the Calendar object Calendar c = Calendar.getInstance (); / / get the current date System.out.println ("current date is" + c.get (Calendar.YEAR) + year + c.get (Calendar.MONTH) + "month" + c.get (Calendar.DAY_OF_MONTH) + "day")) / / increase the year by 2 years c.add (Calendar.YEAR, 2); / / reduce the month by 3 months c.add (Calendar.MONTH,-3); / / get the set date System.out.println ("current date is" + c.get (Calendar.YEAR) + "year" + c.get (Calendar.MONTH) + "month" + c.get (Calendar.DAY_OF_MONTH) + "Day");}}
The result after the code execution
four
Public Date getTime (): returns a Date object that represents the Calendar time value (the millisecond offset from the calendar to the present).
Package demo04;import java.util.Calendar;import java.util.Date;public class Demo04 {/ * public Date getTime (): returns a Date object that represents the Calendar time value (the millisecond offset from the calendar to the present). Convert the calendar object to a date object * / public static void main (String [] args) {/ / use the getInstance method to get the Calendar object Calendar c = Calendar.getInstance (); / / Calendar object-> date object Date date = date object (); System.out.println (date);}}
The result after the code execution
five
Matters needing attention
The week begins on Sunday in the West and Monday in China. In the Calendar class, the month is represented by 0-11 for January-February. The date is related to the size, the time is later, the more time is.
Thank you for your reading, these are the contents of "the principle and usage of Java Calendar Calendar Class". After the study of this article, I believe you have a deeper understanding of the principle and usage of Java Calendar Calendar Class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.