Import Java.text.NumberFormat; Import java.text.ParseException; Import Java.text.SimpleDateFormat; Import Java.util.Calendar; Import Java.util.Date; Import org.apache.commons.lang.time.DateUtils; Import Com.gzdec.common.log.LoggerUtil; /** * This class is used to calculate the interval between two dates, separated by time format as required * @author cxg * */ public class Dateutil { //the number of milliseconds of the day 86400000 = 24*60*60*1000; private static final int millisperday = 86400000; //an hour of milliseconds 3600000 = 24*60*60*1000; private static final int millisperhour = 3600000; /** * Calculate time difference (unit, start time, end time) * Call Method Howlong ("H", "2007-08-09 10:22:26", "2007-08-09 20:21:30")///9 hours 56 minutes return 9 hours & nbsp; * */ public static long Howlong (String unit,string time1,string time2)Throws ParseException { //time units (e.g.: less than 1 days ( 24 hours), return 0, start time, end time Date date1 = new SimpleDateFormat ("Yyyy-mm-dd hh:mm: SS "). Parse (TIME1); Date date2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Parse (time2); Long ltime = Date1.gettime ()-date2.gettime () <0? Date2.gettime ()-date1.gettime ():d ate1.gettime ()-date2.gettime (); if (unit.equals ("s")) { return ltime/1000;//seconds } else if (unit.equals ("M")) { return ltime/60000;//back minutes } Else if (unit.equals ("H")) { return ltime/millisperhour;//hours } else if (unit.equals ("D")) { return ltime/millisperday;//days } Else { return 0; } } /** calculates the difference between the system date and the target date, checks whether the incoming time is earlier than the system time, and then returns "" * @param the target date of tar versus system time * @return * Author: Cxg 2009-5-24 pm 02:06:10 * public Static String getintervalstring (Date tar) { return getintervalstring (New Date (), tar, true); &NBSP;&NBSP;&NBSP;&NBSP} /** calculates the number of days between system date and target date * @param tar Target Date * @return days in contrast to system time, invalid parameter return-1 * author:cxg 2009-5-24 PM 02:16:5 4 * public static int getintervalday (Date tar) { & nbsp; int ret =-1; calendar Calnow = Calendar.getinstance (); if (null!= tar && Tar.before (Calnow.gettime ()) {//parameter effective //to get the calendar at a specified time &nBsp Calendar Caltar = Calendar.getinstance (); caltar.settime (TAR); long MillisNow = Calnow.gettimeinmillis (); long MillisTar = Tar.getTime (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//The specified time is less than the system time to process, otherwise an empty string is returned if (Millistar < MillisNow) { //86400000 = 24*60*60*1000; ret = (int) ( Millisnow-millistar)/(Millisperday)); } } return ret; &NBSP;&NBSP;&NBSP;&NBSP} /** * @param tar target dates compared to system time (string format) * @param formats Resolution date format * @return * A Uthor:cxg 2009-5-24 pm 02:20:11 * public static String Getintervalstring (String tar, string[] formats) { date DTar = null; try { dtar = Dateutils.parsedate (tar, formats); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} catch (ParseException e) { loggerutil.error (Dateutil.class, "Invalid parameters, can ' t format to date!"); e.printstacktrace (); &nbsP; return ""; } return Getintervalstring (Dtar); &NBSP;&NBSP;&NBSP;&NBSP} /** date of day * @param year & nbsp; * @param month * @return * author:cxg 2009-5-24 PM 03: 48:17 * private static int getdaysofmonth (int year, int month) { int day = 0; switch (month) { Semi-rotary case 1: case 3: case 5: case 7: casE 8: case: case A: day = 31; break; //Xiao case 4: case 6: case 9: case: day = 30; break; case 2: if ((0 = year% 400) | | (0 = year% 4 && 0!= year% 100)) {//Leap year day = 29; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} else { day = 28; } break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} // System.out.println ("--nian:" + year + "-month:" + month + "-Days:" + day); return Day; &NBSP;&NBSP;&NBSP;&NBSP} /** calculates the difference of two dates, returns a string such as "1 years February 14 days 11.5 hours", the exception returns "" * @param after * @param before * @return   ; * author:cxg 2009-5-24 PM 03:09:18 * * private static String getintervalstring (date after, date before){ stringbuffer ret = new StringBuffer (); calendar calafter = Calendar.getinstance (); calafter.settime (after); //to get calendar at specified time calendar Calbefore = Calendar.getinstance (); calbefore.settime (before); float hour = 0F; int day = 0; int month = 0; int year = 0; numberformat NF = numberformat.getinstance (); nf.setmaximumfractiondigits (1);//keep 1 decimal // Calculated hours hour = (Calafter.gettimeinmillis ()- Calbefore.gettimeinmillis ())%millisperday)/(float) millisperhour; //Calculation Day day = Calafter.get (Calendar.day_of_month)-Calbefore.get (Calendar.day_of_month); if (Day < 0) { day = day + getdaysofmonth (Calafter.get (calendar.year), Calafter.get (Calendar.MONTH)); month--; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} //Calculation month month = month + calafter.get (calendar.month)-CalBefore.get ( Calendar.month); if (Month < 0) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; month = month + 12; year--; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} //calculation year year = year + calafter.get (calendar.year)-CalBefore.get ( Calendar.year); if (Year > 0) { ret.append (year). Append ("Years"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} if (Month > 0 ) { ret.append (month). Append ("month"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} if (Day > 0) { ret.append (Day). Append ("Days"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP} if (Hour > 0F) { ret.append (Nf.format (Hour)). Append ("hour"); } return Ret.tostring (); &NBSP;&NBSP;&NBSP;&NBSP} /** * Calculates the difference of two dates, returning a string such as "1 years February 14 days", Abnormal situation return "" * @param after * @param before * @par AM check Check for parameter After.before (before), check does not conform to return "" * @return author:cxg 2009-5-24 pm 03:09:18 * public static String getintervalstring (date after, date before, Boolean Check) { if (null!= after && null!= BEFORE) {///parameter valid if (After.before (before)) {// Expect the tar1 date to be larger if ( Check) {//Mandatory check return "" return ""; }else{//Exchange date tmpdate = after; after = before; before = tmpdate; } } return getintervalstring (after, before); }else{ return ""; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} } }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.