Have always wanted to get a blog to show, but also want to record some of the work of the little drops, today began their own blog career, hope that they can persist, refueling! Not much to say, in contact with Java soon, many times recently encountered the conversion and calculation of time, today to do a summary:
1.Date class and Calender class
Date is used to record a time that is accurate to milliseconds, with a date. The emphasis is on representing the time of the flash itself.
Calendar is used to put a date into the calendar of interaction-time and the year, month, day, week, morning, afternoon, summer time and other these calendar provisions of interaction and interaction. The calendar itself represents a simplified shrinking version of the Gregorian calendar.
Common methods for the date class:
Date date0 = new Date ();D ate date1 = new Date (time);
The Get method allows you to obtain specific information such as year, month, day, timestamp, and so on. Note that the getyear () method does not get the current year, but rather gets the difference to the 1900.
Common methods of the Calender class:
Calendar calendar = calendar.getinstance ();D ate Date = Calendar.gettime ();
Gets an instance of a date through the calendar method.
The Calendar method gets the month and day method:
int year =calendar.get (calendar.year); int Month=calendar.get (calendar.month) +1; int day =calendar.get (calendar.day_of_month); int hour =calendar.get (calendar.hour_of_day); int minute =calendar.get (calendar.minute); int seconds =calendar.get (calendar.second);
2. Time and time stamps convert each other
Time conversion to timestamp:
/* * Convert time to timestamp * /public static string Datetostamp (String s) throws parseexception{ string res; SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Date date = Simpledateformat.parse (s); Long ts = Date.gettime (); res = string.valueof (ts); return res; }
Time stamp converted to time:
/* * Convert timestamp to TIME */public static string Stamptodate (string s) { string res; SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Long lt = new Long (s); Date date = new Date (LT); res = Simpledateformat.format (date); return res; }
3. Calculation of Time
3.1 Getting the current time string
/** * Get now time * * @return return string format yyyy-mm-dd HH:MM:SS * /public static string Getstringdate () { Date currenttime = new Date (); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestring = Formatter.format (currenttime); return datestring; }
3.2 Given a date type string, returns the date string that is added and minus n days
public string ndaysafteronedatestring (String Basicdate,int N) {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Date tmpdate = null;
try {
Tmpdate = Df.parse (basicdate);
}
catch (Exception e) {
Date type string format error
}
Long nday= (Tmpdate.gettime ()/(24*60*60*1000) +1+n) * (24*60*60*1000);
Tmpdate.settime (Nday);
Return Df.format (tmpdate);
}
3.3 Returns the date that is added and minus n days after a given date
Public Date ndaysafteronedate (date basicdate,int N) {
Long nday= (Basicdate.gettime ()/(24*60*60*1000) +1+n) * (24*60*60*1000);
Basicdate.settime (Nday);
return basicdate;
}
3.4 Calculate the number of days between two dates
public int ndaysbetweentwodate (String firststring,string secondstring) {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Date Firstdate=null;
Date Seconddate=null;
try {
Firstdate = Df.parse (firststring);
Seconddate=df.parse (secondstring);
}
catch (Exception e) {
Date type string format error
}
int nday= (int) ((Seconddate.gettime ()-firstdate.gettime ())/(24*60*60*1000));
return nday;
}
On the consolidation of time conversion and computation in Java