Time, date

 

 new Date();

 
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//
call the startTime = getNextDate ( "the MM-dd-YYYY 00:00:00", -. 7); // Date days before / ** * get the date and time of the current time interval nDay * @param the format of the return time format of the mM-dd-HH YYYY: mm: SS * @param days interval nday * @return * / public static String getNextDate (the format String, int nday) { calendar calendar = Calendar.getInstance (); // get calendar Date dNow = new new a Date (); // get the current time calendar.setTime (Dnow); // the current time is not assigned calendar Calendar.add (Calendar.DATE, nday); // set nday day intervals (eg: -1 to day before, one day after the + 1) a Date dIntervalDate calendar.getTime = (); // get the time // the SimpleDateFormat SDF the SimpleDateFormat new new = ( "the mM-dd-YYYY HH: mm: SS"); // set the time format the SimpleDateFormat SDF = new new the SimpleDateFormat (the format); // set the time format String time = sdf.format (dIntervalDate); // format of System.out.println ( "************************************************************ Time =" + Time); return Time; } // current date 2019-09- 24, operating results: ********************** time = 2019-09-17 00:00:00

 

In the development process, usually many people are accustomed to using new Date () to get the current time. new Date () doing things is actually called System.currentTimeMillis ().

If only a few milliseconds or need, you can use System.currentTimeMillis () to replace the new Date (), will be higher efficiency. If you need to use new Date multiple times inside the same method (), usually the performance is so consumed little by little, where you can actually declare a reference.

 

 

System.currentTimeMillis():

The effect of the method is to return the current computer time, milliseconds expression format for the current time and computer time GMT time (Greenwich Mean Time) January 1970 No. 10:00:00 as a difference (1s = 1000ms) . This method can be directly cast to date type. code show as below:

Long currentTime = System.currentTimeMillis (); 
the SimpleDateFormat Formatter = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS" ); 

a Date DATE = new new a Date (currentTime); 
a Date date1 = new new a Date (currentTime +. 1 * 60 * 1000 ); 
a Date DATE2 = new new a Date (currentTime. 1-24 * 3600 * 1000 * ); 

System.out.println ( "current time:" + formatter.format (DATE)); 
System.out.println ( "min. 1 current time: "+ formatter.format (date1)); 
System.out.println ( " one day before the current time: "+ formatter.format (date1)) ;

Results are as follows:

Current time: 2019-09-24 11:47:30
current time after 1 minute: 2019-09-24 11:48:30
current time of day before: 2019-09-24 11:48:30

About System.currentTimeMillis () Reference URL: https://www.cnblogs.com/rinack/p/6776100.html



Guess you like

Origin www.cnblogs.com/zdyang/p/11578405.html