In Java, the date and calendar two classes are the main representation of time, and the SimpleDateFormat class used for date and string conversions.
conversion between 0x01:date and string types
the format output and format input of the date type need to be used to SimpleDateFormat class
import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date; SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//date converted to string in the format specified by SDF, using the SDF Format method Date Date = New Date (); String str = sdf.format (date),//string to date, using the Parse method, requires exception handling str = "2014-11-21 10:47:28"; try {date = Sdf.parse (str);} C
Atch (ParseException e) {e.printstacktrace ();}
Conversion between 0x02:date and calendar
Most of the methods in date are obsolete, and date calculations mainly use the calendar.
Import Java.util.calendar;import Java.util.date;//calendar is an abstract class that cannot be directly instantiated, and can return an object of a subclass through its static method getinstance (). Calendar calendar = calendar.getinstance ();//Use the GetTime method to convert the calendar to datedate date = Calendar.gettime ();// Use the SetTime method to convert date to calendarcalendar.settime (date);
Calculation and conversion of time and date in Java