Time formatted a few minutes ago a few hours ago

Source: Internet
Author: User
/** * Time of 24 hours display format/public class Formatdateutils {/**////////////////////////////////24 hours before
     More than 24 hours three days (including three days) display a few days ago,//3 days above the display of the specific date, to the day.

        Year displayed for more than one year, year not shown * @param * @return/public static string FormatDate (string para) {

        Now the time Integer nowyear = Integer.parseint (DateTime.Now (). toString ("yyyy")); The time format datetimeformatter format = Datetimeformat.forpattern ("Yyyy-mm-dd HH:mm:ss.

        S "); Time to parse DateTime paradate = DateTime.Parse (Para,format);//month day time seconds Integer parayear = Integer.parseint (P


        Aradate.tostring ("yyyy"));//year String Paraday = paradate.tostring ("Yyyy-mm-dd");   DateTime nowdate = new DateTime (). now ();
        Current Interval hours1 = new Interval (nowdate.minushours (1), nowdate);
        Interval Hours24 = new Interval (nowdate.minushours), nowdate);
    Interval day = new Interval (Nowdate.minusdays (4), nowdate);    Interval years = new Interval (Nowdate.minusyears (1), nowdate);
            if (Hours1.contains (paradate)) {//within one hour display minutes ago; Interval minute = new Interval (paradate, nowdate);
            int res = Minute.toperiod (). getminutes ();
            if (res = = 0 | | res = = 1) {return "just";
           return res+ "Minutes ago";
        Return "a few minutes ago";
            else if (Hours24.contains (paradate)) {//24 hours before the display, Interval hours = new Interval (paradate, nowdate);
            int res = Hours.toperiod (). GetHours ();
            return res+ "hours ago";
        Return "A few hours ago";
            else if (Day.contains (paradate)) {//More than 24 hours three days (including three days) display a few days ago; Interval day = new Interval (paradate, nowdate);
            int res = Day.toperiod (). GetDays ();
        return res+ "days ago";
        }else if (Parayear < nowyear) {return paraday;
        }else {return paradate.tostring ("Mm-dd"); }
    }
}

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.