1 PackageCom.tem1.util;2 3 Importjava.text.ParsePosition;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 Public classIntervalutil {8 9 PublicString Getinterval (String createtime) {//The incoming time format must resemble the format of 2012-8-21 17:53:20TenString interval =NULL; One ASimpleDateFormat SD =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); -Parseposition pos =NewParseposition (0); -Date D1 =(Date) sd.parse (Createtime, POS); the - //with the current distance from 1970 to the time interval new - //date (). GetTime () minus the time interval from the previous time distance of 1970 D1.gettime () is the time interval between the previous time and the current time - LongTime =NewDate (). GetTime ()-d1.gettime ();//The resulting time interval is milliseconds. + - if(time/1000 < && time/1000 >= 0) { + //If the time interval is less than 10 seconds, the "just" Time/10 time interval is displayed in seconds [0 seconds, 10 seconds) AInterval = "just"; at -}Else if(time/1000 < && time/1000 >=10) { - //how many seconds before [10 seconds-60 seconds] If the time interval is less than 60 seconds - intSE = (int) ((Time% 60000)/1000); -Interval = se + "seconds ago"; - in}Else if(time/60000 < && time/60000 >=1) { - //how many minutes before [1 minutes, 60 Minutes] If the time interval is less than 60 minutes to intm = (int) ((time% 3600000)/60000);//The time interval is measured in minutes +Interval = m + "Minutes ago"; - the } * Else if(time/3600000 < && time/3600000 >= 1) { $ //how many hours before [1 hours, 24 hours] If the time interval is less than 24 hoursPanax Notoginseng intH = (int) (time/3600000);//The time interval is measured in hours. -Interval = h + "Hour Ago"; the +}Else { A //More than 24 hours, the normal time is displayed, but the seconds are not displayed theSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd hh:mm"); + -Parseposition Pos2 =NewParseposition (0); $Date D2 =(Date) sdf.parse (Createtime, pos2); $ -Interval =Sdf.format (D2); - } the returninterval; - Wuyi } the - Public Static voidMain (string[] args) { WuIntervalutil interval =Newintervalutil (); -String date1 = "2014-10-11 10:56:17"; AboutString date2 = "2014-10-10 09:39:17"; $String date3 = "2014-10-11 05:49:17"; -String date4 = "2014-10-11 10:01:11"; - System.out.println (Interval.getinterval (date1)); - System.out.println (Interval.getinterval (Date2)); A System.out.println (Interval.getinterval (Date3)); + System.out.println (Interval.getinterval (Date4)); the - } $}
Java Publishing time interval