A few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago Java implementation

Source: Internet
Author: User
Tags string format

Code Download: http://www.zuidaima.com/share/1562038902000640.htm

On the StackOverflow to find a simple, rewritten under the requirements, to share with you.

Package Com.zuidaima;
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 ("yyy
        Y-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;


 }

}

Need to download, you can modify your test code test.


Reprint please be sure to indicate the source


Related Article

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.