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