Java Time Operations

Source: Internet
Author: User

1.2 Common time-of-day operations:

Import java.util.Date; import java.util.Calendar; import Java.text.SimpleDateFormat; Public classtestdate{ Public Static voidMain (string[] args) {Date now=NewDate (); SimpleDateFormat DateFormat=NewSimpleDateFormat ("YYYY/MM/DD HH:mm:ss");//easy to modify date formatString hehe=Dateformat.format (now); System. out. println (hehe); Calendar C= Calendar.getinstance ();//can be modified individually for each time domainintYear = c.Get(calendar.year);intmonth = c.Get(calendar.month);intDate = c.Get(calendar.date);inthour = c.Get(calendar.hour_of_day);intminute = c.Get(Calendar.minute);intSecond = c.Get(Calendar.second); System. out. println (Year +"/"+ Month +"/"+ Date +" "+hour +":"+minute +":"+second); Sometimes you convert a string of time to a date type, and you can convert the time string you just got to date type in the following way. SimpleDateFormat SDF=NewSimpleDateFormat ("YYYY-MM-DD"); Java.util.Date Time=NULL;Try{ Time= Sdf.parse (Sdf.format (NewDate ()));} Catch(ParseException e) {e.printstacktrace ();}

2. Date comparison:

Calendar C =calendar.getinstance ();inthour = c.Get(calendar.hour_of_day);intminute = c.Get(Calendar.minute); DateFormat DF=NewSimpleDateFormat ("hh:mm");D ate d1= Df.parse ("11:31");//String to date D2= Df.parse (" the");Longdiff = d1.gettime ()-d2.gettime ();//Here diff is a long shape number, if D1 than D2, is positive, such as this example is positive, the inverse is a complex, equal to 0System. out. println (diff);

Java Time Operations

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.