Java a few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago realized

Source: Internet
Author: User
Tags string format

Original: Java a few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago realized

Source code: Http://www.zuidaima.com/share/1562038902000640.htm

Previous code shared by cattle people:

Java implementation a few minutes ago, a few hours ago, a few days before the code

A bit complicated, so on the StackOverflow found a simple, rewrite the next meet the needs, to share the next.

Package Com.date;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date;public    Class Relativedateformat {private static final long One_minute = 60000L;    Private static final long one_hour = 3600000L;    Private static final long one_day = 86400000L;    Private static final long One_week = 604800000L;    private static final String One_second_ago = "seconds ago";    private static final String One_minute_ago = "Minutes ago";    private static final String One_hour_ago = "Hour Ago";    private static final String One_day_ago = "days ago";    private static final String One_month_ago = "month ago";    private static final String One_year_ago = "Years ago"; public static void Main (string[] args) throws parseexception {SimpleDateFormat format = new SimpleDateFormat ("yyyy        -mm-dd hh:m:s ");        Date date = Format.parse ("2013-11-11 18:35:35");    SYSTEM.OUT.PRINTLN (Format (date)); public static String format (date date) {Long delta = new Date (). GetTime ()-Date.gettiMe ();            if (Delta < 1L * One_minute) {Long seconds = toseconds (delta);        return (seconds <= 0? 1:seconds) + One_second_ago;            } if (Delta < 45L * One_minute) {Long minutes = tominutes (delta);        Return (minutes <= 0? 1:minutes) + One_minute_ago;            } if (Delta < 24L * One_hour) {Long hours = tohours (delta);        Return (Hours <= 0? 1:hours) + One_hour_ago;        } if (Delta < 48L * One_hour) {return "Yesterday";            } if (Delta < 30L * One_day) {Long days = todays (delta);        Return (days <= 0 1:days) + one_day_ago;            } if (Delta < 12L * 4L * one_week) {Long months = tomonths (delta);        Return (months <= 0? 1:months) + One_month_ago;            } else {Long years = Toyears (delta);        Return (Years <= 0? 1:years) + One_year_ago; }} private StatiC Long Toseconds (long date) {return date/1000l;    } private static Long tominutes (long date) {return toseconds (date)/60L;    } private static Long tohours (long date) {return tominutes (date)/60L;    } private static long todays (long date) {return tohours (date)/24L;    } private static Long tomonths (long date) {return todays (date)/30L;    } private static Long Toyears (long date) {return tomonths (date)/365L; }}

There is a need to download, you can modify the test code test.


Java a few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago realized

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.