The Java.util.GregoiranCalendar of the Java Time Operation trilogy

Source: Internet
Author: User
Tags add days locale

We have already introduced its ancestors date and Father calendar, today to meet gregoriancalendar. In the java1.0 version of the date is still very powerful, but the Java class Library designers think that the time and the Time-point name of the class to separate, so there is a time to represent the date and the familiar calendar representation of the Gregorianclendar. The previous article introduced the calendar, in fact Today's protagonist is to expand the calendar, become more General.

class Constructors

1.GregorianCalendar() constructs a default in the default time zone with the default locale using the current time GregorianCalendar .

2.GregorianCalendar(int year, int month, int Dayofmonth) , as well as overloaded methods, the order of the parameters has a large order of time points: years, months, days, hours, minutes, SECONDS. Constructs a GregorianCalendar with the given date and time settings for the default time zone with the default Locale.

New // 2016-11-06

3.GregorianCalendar(Locale Alocale) Constructs a default time zone with a given locale based on the GregorianCalendar。(包括一下两个构造函数,在近期开发中基本没有使用过,后面用了在更新吧) current

4.GregorianCalendar(TimeZone zone) constructs one based on the current time in a given time zone with a default locale GregorianCalendar .

5.GregorianCalendar(TimeZone zone, Locale alocale) constructs a current-time -based GregorianCalendar.

Class method

the GregorianCalendar class inherits from the calendar class, so most of the methods we use are already described in the previous article, and here are just a few important ways.

The function of a calendar is to provide a point in time for the year, month, and day How this information is captured in the GregorianCalendar class, called the Get method, and uses some of the constants in the calendar, for example:

 public Static voidmain (string[] Args) {//TODO auto-generated Method StubGregorianCalendar now=NewGregorianCalendar (); intYear =Now.get (calendar.year); intmonth = Now.get (calendar.month) +1; intDay =Now.get (calendar.day_of_month); System.out.println ("current date:" +year+ "year" +month+ "month" +day+ "day");//current date: November 6, 2016        ////set a new timeNow.set (calendar.year,1991); Now.set (calendar.month,5); Now.set (calendar.day_of_month,20); System.out.println (now);
is not looking at the following paragraph is a bit familiar, yes, in the last article Said. //java.util.gregoriancalendar[time=?,arefieldsset=false,areallfieldsset=true,lenient=true,zone= Sun.util.calendar.zoneinfo[id= "asia/shanghai", offset=28800000,dstsavings=0,usedaylight=false,transitions=19, Lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=1991,month=5,week_of_year=46,week_of_month =2,day_of_month=20,day_of_year=311,day_of_week=1,day_of_week_in_month=1,am_pm=1,hour=8,hour_of_day=20,minute=1 , second=54,millisecond=363,zone_offset=28800000,dst_offset=0] }

You can also add days, months, etc. to the current time, for example:

Now.add (calendar.month,2); // the positive number increases, The negative number decreases, this sentence means: increase three months

Use this class to write a calendar that displays the current month

     public Static voidmain (string[] Args) {//Create the current date objectGregorianCalendar now =NewGregorianCalendar (); //get current month, current day        intNowmonth =Now.get (calendar.month); intNowday =Now.get (calendar.day_of_month); //set to the first day of the current monthNow.set (calendar.day_of_month,1); //know the first day of the month is a few weeks        intWeekday =Now.get (calendar.day_of_week); //get the start of the current region week        intFirstDayOfWeek =Now.getfirstdayofweek (); //calculate indentation on first week display        intIndent=0;  while(weekday! =Firstdayofweek) {indent++; Now.add (calendar.day_of_month,-1); Weekday= Now.get (calendar.day_of_week);//This one day will go to the last day of last month, and so on, to find and the day of the beginning of the week, the brain to fill the calendar, want to see the last month of the day of the week                    }                //prepare to print the week portion of the calendarstring[] Weeknames =Newdateformatsymbols (). getshortweekdays ();  do{//at first I did not understand this do loop, in the above code we have reduced the date to the current region of the day of the week start, so first cycle, so that in a lap can be printed correctly seven weeks of the abbreviationSystem.out.printf ("%6s", weeknames[weekday]); Now.add (calendar.day_of_month,1); Weekday=Now.get (calendar.day_of_week); }  while(weekday! =firstdayofweek);                System.out.println ();  for(inti = 0; I < indent; i++) {System.out.printf ("%7s", "" ");//Printing} now.set (calendar.day_of_month,1);//return to the first day of the month                 do{            intday=Now.get (calendar.day_of_month); System.out.printf ("%6d", day); if(day = =Nowday) System.out.print ("*"); ElseSystem.out.print (" "); Now.add (calendar.day_of_month,1); Weekday=Now.get (calendar.day_of_week); if(weekday = =Firstdayofweek)        System.out.println (); }  while(now.get (calendar.month) = =nowmonth); }

well, This type is introduced here, do not know this thing, the input method suddenly become a complex word, I was in the middle of the mainland people, Hahaha.

The Java.util.GregoiranCalendar of the Java Time Operation trilogy

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.