Use Java operation time feeling real egg hurts, or My big C # comfortable, a datetime all done
The Date here refers to Java.util.Date.
Get current time:
Creates a date object of the current time date times = new Date ();
Where the egg hurts, increase and decrease the operation of the time:
Use the Calendar class to increase or decrease the time of the calendar C = calendar.getinstance ();//To obtain a calendar instance, which is an abstract class, so you cannot use the new constructor method // Use the SetTime method to create a time, which is the date type c.settime. Adds 12 months to the current time, which can be changed to add Unit c.add (Calendar.month, +) According to the Calendar enumeration value; Convert Calendar to date object Date DateTime = C.gettime ();
Again the place where the egg hurts, format the time, convenient for people to look at the format:
Use SimpleDateFormat to format the time as a string timestr = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (timing);
Simple operation, Memo:
Convenient timestamp conversions:
/** * Turn time objects into timestamps * @param time * 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 timestamp to time object * * @param time * timestamp * @return Time Object */public static Date Longtodate (long time) { date date = null; try { date = new Date (time); SYSTEM.OUT.PRINTLN (date); } catch (Exception e) { e.printstacktrace (); } return date; }
Java Time Simple operation