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 New Date ();
Where the egg hurts, increase and decrease the operation of the time:
// Use the Calendar class for time increment, subtraction operations Calendar C = calendar.getinstance (); // get a calendar instance, which is an abstract class so you cannot use the new construct method // use the SetTime method to create a time that is a date type C.settime (time); // Adds 12 months to the current time, which can be changed to add units based on the Calendar enumeration value C.add (Calendar.month,N); // 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 time as a string New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (time);
Simple operation, Memo:
Convenient timestamp conversions:
/*** Turn the time object into a timestamp * *@paramTime * *@returntime Stamp*/ Public Static LongDatetolong (Date time) {Try { LongTimel =Time.gettime (); System.out.print (Timel); returnTimel; } Catch(Exception e) {e.printstacktrace (); return0; } } /*** Convert timestamp to TIME object * *@paramtime * Timestamp *@returnTime Object*/ Public StaticDate Longtodate (LongTime ) {Date Date=NULL; Try{Date=NewDate (time); SYSTEM.OUT.PRINTLN (date); } Catch(Exception e) {e.printstacktrace (); } returndate; }
Java Time Simple operation