Java time simple operation, java operation
It's really cool to use java to operate on time, or I am still comfortable with C #, all done with a DateTime
Here, Date refers to java. util. Date.
Get current time:
// Create a Date object of the current time Date time = new Date ();
To increase or decrease the time when the eggs hurt:
// Use the Calendar class to increase or decrease the time. getInstance (); // get a Calendar instance. This class is an abstract class, so you cannot use the new constructor. // use the setTime method to create a time. This time is of the Date type c. setTime (time); // increase the current time by 12 months. You can change the Unit c according to the Calendar enumerated value. add (Calendar. MONTH, 12); // convert Calendar to Date object Date dateTime = c. getTime ();
Once again, the time is formatted to help you see the format:
// Use SimpleDateFormat to format the time String in the format of String timeStr = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss"). format (time );
Simple operation, Memo:
Convenient timestamp conversion:
/*** Convert a time object to a timestamp ** @ param time * @ return timestamp */public static long DateToLong (Date time) {try {long timeL = time. getTime (); System. out. print (timeL); return timeL;} catch (Exception e) {e. printStackTrace (); return 0 ;}} /*** convert the timestamp into a time object ** @ param time * timestamp * @ return time object */public static Date LongToDate (long time) {Date date Date = null; try {date = new Date (time); System. out. println (date);} catch (Exception e) {e. printStackTrace ();} return date ;}