Time class in Java

Source: Internet
Author: User
1. Similarities and Differences Between calendar and date

    • The date class is an older class, and many methods in it should be discarded.A specific subclass of dateformat. simpledateformat is used in combination with the time format conversion.

 

 
Simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); dateformat. format (new date (); // The returned result is 11:16:34. 988: D/(16341): 11:16:34
 
 

    • The date class is an operation on the current time. Therefore, the calendar class uses the calendar. Set (year, month, day) method for the operation of the specified time to set the specified time. Calendar. Add (field, value) is used to set the advance or delay time based on the value. For example, calendar. Add (calendar. date, 6); indicates 6 days later.
2. Based on the three methods described above, the instance can obtain the following 10-01 ~ 10-11

CodeAs follows:

 
Public static string [] getweekarray () {string [] datearray; Calendar calendar = gregoriancalendar. getinstance (); datearray = new string [calendar. get (calendar. week_of_year)]; simpledateformat SDF = new simpledateformat ("M-dd"); For (INT I = 0; I <datearray. length; I ++) {string date1 = SDF. format (calendar. gettime (); Calendar. add (calendar. date,-6); string date = SDF. format (calendar. gettime (); datearray [I] = Date + "~ "+ Date1; Calendar. Add (calendar. Date,-1);} return datearray ;}

The output is as follows:

3. I checked the long timestamp online and found a lot of Bala and I don't know what to do. I don't have this requirement currently, so I only need to paste the timestamp and date class conversion

Simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); long time = new long (445555555); string d = format. format (time); Date = format. parse (d); system. out. println ("format to string (date):" + d); system. out. println ("format to date:" + date );

Result

Format to string (date): 11:45:55
Format to date: Tue Jan 06 11:45:55 CST 1970

3. Set a comparison between the time and the current time

year = 1 monthofyear = 2 dayofmonth = 29
for example, set calendar. set (year, monthofyear, dayofmonth);
current time gregoriancalendar. getinstance ();
Print result Current Time Value Fri Mar 29 14:02:19 GMT + 0800 2013
my set time value Fri Mar 29 14:02:19 GMT + 0800 2013
comparison calendar. gettime ()! = Gregoriancalendar. getinstance (). gettime ()
actually, the difference between them is several milliseconds. Only when the timestamp is printed can we find that
the correct comparison method

string date1 = dateformat. format (calendar. gettime ();
string date2 = dateformat. format (currentcalendar. gettime ();
If (dateformat. parse (date1 ). compareto (dateformat. parse (date2) = 0) {
Print ("Equal Time ");
return;
}< br> extract the date and then compare it.
4. date and calendar convert each other

Conversion of calendar and date (1) convert calendar to datecalendar Cal = calendar. getinstance (); Date = Cal. gettime (); (2) convert date to calendardate date = new date (); Calendar ar Cal = calendar ar. getinstance (); Cal. settime (date );

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.