Summary of Date Processing in java
This article summarizes the date processing method before jdk8. the new date Processing Method in jdk8.
Package com. test; import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; public class DateAndTime {public static void main (String [] args) {int year; int month; int day; // old version Date date = new Date (); year = date. getYear () + 1900; month = date. getMonth () + 1; day = date. getDate (); System. out. println ("old --" + year + "year" + month + "month" + day + "day"); // the new version of Calendar calendar = Calendar. getInstance (); year = calendar. get (Calendar. YEAR); month = calendar. get (Calendar. MONTH) + 1; day = calendar. get (Calendar. DATE); System. out. println ("New --" + year + "year" + month + "month" + day + "day"); // The number of milliseconds long now = System. currentTimeMillis (); Date date2 = new Date (now); // or: date. setTime (now); long --> Date System. out. println (date2.getDate (); // time type conversion long <-> Date <-> Calendar calendar. setTime (date2); // Date --> Calendar System. out. println (calendar. get (Calendar. YEAR); calendar. getTime (); // Calendar --> Date System. out. println (calendar. getTime (). getDate (); date. getTime (); System. out. println (date. getTime (); // Date --> long // time input and output SimpleDateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); string today = fmt. format (date2); // Date --> String try {Date date3 = fmt. parse ("16:20:00"); // String --> Date System. out. println (date3);} catch (ParseException e) {e. printStackTrace ();} System. out. println (today); // use the Calendar time to calculate the calendar. add (Calendar. YEAR, 2); calendar. add (Calendar. MONTH, 1); System. out. println (fmt. format (calendar. getTime (); // Today is July 22, December 19, 2015. This should be July 15, January 19, 2018 }}
The console is printed as follows:
Old -- New in December 20152015 -- 192015191450513438352Sat Dec 19 16:20:00 CST 582018-12-19 16: 23:-01-19 16:23:58