1. Get Calendar instance: Calendar c = calendar.getinstance ();
2. Define date/Time format: SimpleDateFormat SDF =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
3. Convert date/Time to a fixed format using the SimpleDateFormat format () method:
String datetime = Sdf.format (C.gettime ());
4. Converts the string to date/time using the SimpleDateFormat Parse () method: Date D = Sdf3.parse ("2016-08-08 16:43:00");
5. Date/time increase, decrease, use the Calendar Add () method, such as reducing the date by 100 days: C.add (calendar.date,-100);
6. Date/Time settings, using the calendar's set () method, such as setting the hour to 0 o'clock:
C.set (calendar.hour_of_day, 0);
Example:
Package MyCalendar;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date; public class MyCalendar {public static void main (String args[]) throws exception{Calendar C = calendar.getinstance
();
SimpleDateFormat sdf1 =new SimpleDateFormat ("YyyyMMdd");
SimpleDateFormat sdf2 =new SimpleDateFormat ("Hhmmss");
SimpleDateFormat sdf3 =new SimpleDateFormat ("Yyyymmddhhmmss");
SimpleDateFormat sdf4 =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String date = Sdf1.format (C.gettime ());
SYSTEM.OUT.PRINTLN (date);
String time = Sdf2.format (C.gettime ());
System.out.println (time);
String dt = "20160808162405";
Date d = sdf3.parse (DT);
DT = Sdf4.format (d);
C.settime (d);
C.add (calendar.date,-100);
C.set (calendar.hour_of_day, 0);
C.set (calendar.minute, 0);
C.set (Calendar.second, 0);
SYSTEM.OUT.PRINTLN ("100 days ago:" + Sdf4.format (C.gettime ())); C.add (Calendar.DATE, 200);
C.set (calendar.hour_of_day, 0);
C.set (calendar.minute, 0);
C.set (Calendar.second, 0);
SYSTEM.OUT.PRINTLN ("100 Days later:" + Sdf4.format (C.gettime ())); }
}
The above is the Java date time operation method of all the contents of the introduction, I hope to help.