Java Api--calendar Class

Source: Internet
Author: User

1. Calendar class overview

The Calendar class is an abstract class that is a specific moment with a set of such things as year, MONTH, Day_of_month, HOUR, etc.

Conversion between calendar fields provides methods and provides methods for manipulating calendar fields, such as getting the date of the next week.

2. Member Methods

public static Calendar getinstance (): Initialize, Return child class object

public int get (int field): Returns the value of the given calendar field

public void Add (int field,int amount): Operates on the current calendar based on the given calendar field and the corresponding time

Public final void Set (int year,int month,int date): Sets the month and day of the current calendar Example 1:
/** Calendar: It provides methods for converting between specific moments and a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and provides methods for manipulating calendar fields, such as getting the date of the next week. * public int get (int field): Returns the value of the given Calendar field. Each calendar field in the Calendar class is a static member variable and is of type int. */ Public classCalendardemo { Public Static voidMain (string[] args) {//its calendar field has been initialized by the current date and time:Calendar RightNow = Calendar.getinstance ();//sub-class object//Get year        intYear =Rightnow.get (calendar.year); //Get Month        intmonth = Rightnow.get (calendar.month);//counting from 0 onwards//Get Day        intDate =Rightnow.get (calendar.date); System.out.println ( Year+ "Year" + (month + 1) + "Month" + Date + "Day"); }}

Example 2:

ImportJava.util.Calendar;/** public void Add (int field,int amount): Operates on the current calendar based on the given calendar field and the corresponding time. * Public final void set (int year,int month,int date): Sets the month and day of the current calendar*/ Public classCalendarDemo02 { Public Static voidMain (string[] args) {Calendar C= Calendar.getinstance ();//sub-class object//Get year        intYear =C.get (calendar.year); //Get Month        intmonth = C.get (calendar.month);//counting from 0 onwards//Get Day        intDate =C.get (calendar.date); System.out.println ( Year+ "Year" + (month + 1) + "Month" + Date + "Day"); //3 years ago today.C.add (calendar.year,-3); year=C.get (calendar.year); //Get Monthmonth =C.get (Calendar.month); //Get DayDate =C.get (calendar.date); System.out.println ( Year+ "Year" + (month + 1) + "Month" + Date + "Day"); //5 years later, 10 days agoC.add (calendar.year,5); C.add (Calendar.date,-10); year=C.get (calendar.year); //Get Monthmonth =C.get (Calendar.month); //Get DayDate =C.get (calendar.date); System.out.println ( Year+ "Year" + (month + 1) + "Month" + Date + "Day"); //Set the timeC.set (2011,11,11); year=C.get (calendar.year); //Get Monthmonth =C.get (Calendar.month); //Get DayDate =C.get (calendar.date); System.out.println ( Year+ "Year" + (month + 1) + "Month" + Date + "Day"); }}

Example 3: Get the February of any year and how many days

ImportJava.util.Calendar;ImportJava.util.Scanner;/** Get how many days in February of any year * * Analysis: * A: Keyboard input any year * B: Set Calendar Object Month Day * year is a input data * month is 2 * Day is 1 * C: Push the time forward one day, the last day of February * D: Get this day output can be*/ Public classCalendarDemo03 { Public Static voidMain (string[] args) {//Keyboard entry for any yearScanner sc =NewScanner (system.in); System.out.println ("Please enter the year:"); intYear =Sc.nextint (); //set Calendar object's Month dayCalendar C =calendar.getinstance (); C.set (Year,2, 1);//This is actually the March 1 of the year.//Push the time forward one day, the last day of FebruaryC.add (Calendar.date, 1); //get this day's outputSystem.out.println (C.get (calendar.date)); }}

Java Api--calendar Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.