Calculation of time interval in Java (the previous time interval is similar to the current time when posting on Sina Weibo)

Source: Internet
Author: User
Import Java. text. parseposition; import Java. text. simpledateformat; import Java. util. date; public class intervalutil {Public String getinterval (string createtime) {// The input time format must be similar to the format string interval = NULL such as 17:53:20; simpledateformat SD = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); parseposition Pos = new parseposition (0); Date d1 = (date) SD. parse (createtime, POS); // use the current time interval from January 1, 1970 to new date (). gettime () minus the time interval from January 1, 1970 d1.gettime (), it is obtained that the time interval between the previous time and the current time is long time = new date (). gettime ()-d1.gettime (); // The time interval obtained is millisecond if (Time/1000 <10 & time/1000> = 0) {// If the interval is less than 10 seconds, the system displays the time interval "just" Time/10 ". The unit of the time interval is second interval =" just ";} else if (Time/3600000 <24 & time/3600000> = 0) {// The number of hours before which int H = (INT) is displayed if the time interval is less than 24 hours) (Time/3600000); // The unit of the obtained time interval is hour interval = H + "before Hour ";} else if (Time/60000 <60 & time/60000> 0) {// if the time interval is less than 60 minutes, the number of minutes before int M = (INT) is displayed) (time % 3600000)/60000); // The unit of the obtained time interval is minute interval = m + "Minutes Ago ";} else if (Time/1000 <60 & time/1000> 0) {// if the time interval is less than 60 seconds, the number of seconds before the display int Se = (INT) (time % 60000)/1000); interval = Se + "seconds ago";} else {// greater than 24 hours, the normal time is displayed, however, the second simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm"); parseposition pos2 = new parseposition (0); Date D2 = (date) SDF is not displayed. parse (createtime, pos2); interval = SDF. format (D2) ;}return interval ;}}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.