Java date and time

Source: Internet
Author: User

-- Start

Java uses the date class to represent the date and time. Since JDK 1.1, most of the methods in the date class are no longer recommended because these methods cannot meet international requirements. instead, the calendar class is used to operate the date, and the dateformat class is used to format the date.
In the next section, I will introduce the related knowledge about number and date formatting. This section focuses on date operations. obviously, to operate on dates, we often need to convert between the date and calendar classes. the following is a simple example.

Public static void main (string [] ARGs) throws exception {// defines the current date today = new date (); system. out. println ("today is" + today); // convert date to calendarcalendar c = calendar. getinstance (); C. settime (today); // Add a day to the date C. add (calendar. day_of_month, 1); // convert the calendar to date Date Tomorrow = C. gettime (); system. out. println ("torrow is" + tomorrow );}

What operations do we usually want to perform on the date?
First, we must be ableDefine a specific date;
Sometimes we needObtain the components of a date.Such as year, month, day, hour, minute, and second;
Sometimes we also needCompare the sizes of two dates;
Sometimes we needCalculate the interval between two datesSuch as year, month, day, hour, minute, and second;
Sometimes we needResult of adding or subtracting N years, months, days, hours, minutes, And seconds to a specific date;
In some casesTime zone conversion.

Next we will use a specific example to see how to operate on the date.

Public static void main (string [] ARGs) throws exception {// defines a specific date system. out. println ("------ define a specific date ------"); Calendar c = calendar. getinstance (); C. set (2012, 4, 31, 17, 02, 20); // 2012-05-31 17: 02: 20system. out. println (C. gettime (); system. out. println ("\ n"); // obtain the components of the date system. out. println ("------ get the various components of the date ------"); system. out. println ("year:" + C. get (calendar. year); system. out. println ("month:" + (C. get (calendar. month) + 1); // January is 0system. out. println ("day:" + C. get (calendar. day_of_month); system. out. println ("hour:" + C. get (calendar. hour_of_day); system. out. println ("points:" + C. get (calendar. minute); system. out. println ("seconds:" + C. get (calendar. second); system. out. println ("Week:" + (C. get (calendar. day_of_week)-1); // Sunday is 1system. out. println ("\ n"); // compare the size of two dates: system. out. println ("------ compare the size of two dates ------"); Calendar now = calendar. getinstance (); If (now. before (c) {system. out. println (now. gettime () + "in" + C. before gettime () +. ");} If (now. after (c) {system. out. println (now. gettime () + "in" + C. after gettime () +. ");} If (now. compareto (c)> 0) {system. out. println (now. gettime () + "greater than" + C. gettime ();} system. out. println ("\ n"); // calculate the interval between two dates. out. println ("------ calculate the interval between two dates ------"); long dateinterval = math. ABS (C. gettime (). gettime ()-now. gettime (). gettime ()/(1x24x60*60*1000); system. out. println (now. gettime () + "and" + C. the time interval for gettime () + "is" + dateinterval + "day. "); system. out. println ("\ n"); // calculate the result system after adding or subtracting N years, months, days, hours, minutes, And seconds to a specific date. out. println ("------ calculation result after adding or subtracting 1 year/month/day/hour/minute/second to a specific date ------"); C. add (calendar. year, 1); // Add 1 year system. out. println ("plus 1 year:" + C. gettime (); C. add (calendar. month, 1); // Add January system. out. println ("plus January:" + C. gettime (); C. add (calendar. day_of_month, 1); // Add 1 day system. out. println ("plus 1:" + C. gettime (); C. add (calendar. day_of_month, 32); // Add the 32-day system. out. println ("plus 32:" + C. gettime (); C. add (calendar. hour_of_day, 1); // Add 1 to system. out. println ("plus 1:" + C. gettime (); C. add (calendar. minute, 1); // Add 1 point to system. out. println ("+ 1:" + C. gettime (); C. add (calendar. second, 1); // Add 1 second system. out. println ("plus 1 second:" + C. gettime (); system. out. println ("\ n"); // time zone conversion C. clear (); // clear settings C. set (2012, 4, 31, 17, 02, 20); // 2012-05-31 17: 02: 20system. out. println ("------ time zone conversion ------"); system. out. println ("Current Time Zone:" + C. gettimezone (). getdisplayname (); system. out. println ("current time:" + C. get (calendar. year) + "-" + (C. get (calendar. month) + 1) + "-" + C. get (calendar. day_of_month) + "" + C. get (calendar. hour_of_day) + ":" + C. get (calendar. minute) + ":" + C. get (calendar. second); C. settimezone (timezone. gettimezone ("GMT"); system. out. println ("converted Time Zone:" + C. gettimezone (). getdisplayname (); system. out. println ("converted time:" + C. get (calendar. year) + "-" + (C. get (calendar. month) + 1) + "-" + C. get (calendar. day_of_month) + "" + C. get (calendar. hour_of_day) + ":" + C. get (calendar. minute) + ":" + C. get (calendar. second ));}

The running result is as follows:

------ Define a specific date ------ Thu May 31 17:02:20 CDT 2012 ------ get the various components of the date ------ year: 2012 month: 5 day: 31 hour: 17 minute: 2 seconds: week 20: 4 ------ compare the sizes of two dates ------ Fri May 04 00:41:19 CDT 2012 before Thu May 31 17:02:20 CDT 2012. ------ calculate the interval between two dates ------ Fri May 04 00:41:19 CDT 2012 and Thu May 31 17:02:20 CDT 2012 are separated by 27 days. ------ calculation result after adding or subtracting 1 year/month/day/hour/minute/second to a specific date ------ plus 1 year: Fri May 31 17:02:20 CDT 2013 plus January: sun Jun 30 17:02:20 CDT 2013 plus 1: Mon Jul 01 17:02:20 CDT 2013 plus 32: Fri Aug 02 17:02:20 CDT 2013 plus 1: Fri Aug 02 18:02:20 CDT 2013 plus 1: fri Aug 02 18:03:20 CDT 2013 plus 1 second: Fri Aug 02 18:03:21 CDT 2013 ------ time zone conversion ------ Current Time Zone: Central Standard Time Current Time: After time zone conversion: greenwich Mean Time After conversion:

From the above example, we can see that the calendar class does not provide a method for calculating the date interval, and we need to manually calculate it.

Note that we add 32 days to the date in the 58 rows in the preceding example, but the program does not throw an exception because the calendar class uses the loose (leniency) computing method by default, of course, we can also change it to a strict mode by calling the following method. At this time, if we run the 58-line code program again, an exception will be thrown.

c.setLenient(false);

We can see from the 79 rows in the above example that we can get a timezone instance in the following way, but we need to specify an ID. The question is, how can we determine whether an ID is legal? In fact, it is very simple. We can call the getavailableids () method to view it.

TimeZone.getTimeZone("GMT")

Through the above example, we can see that using the calendar class to operate on dates is really complicated. Therefore, many open-source date frameworks provide easier-to-use and more powerful functions, such as joda.

---For more information, see:Java
--Shengming: reprinted, please indicate the source
-- Last updated on 2012-05-04
-- Written by shangbo on 2012-05-03
-- End

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.