Micro-letter and QQ time format template Examples of detailed knowledge of the basics

Source: Internet
Author: User
Tags current time

        directly on the code, there are comments in the code, we look good!

/** * Converts a timestamp to a prompt time string, such as * no display within 2 minutes * 2 minutes-24 hours hh:mm * Yesterday yesterday hh:mm * hh:mm * A year ago Mm:dd hh:mm * Last year mm:dd hh:mm * MM:DD hh:mm * Further yyyy:MM:DD hh:mm * Millisecond calculation * @param charttime * @return * * p
    Ublic static String Convertchatdetailtimeformat (long charttime) {Long curtime = System.currenttimemillis ();
 
    Long time = Curtime-charttime;
    XCAPPLICATION.BASE_LOG.I (Xcconfig.tag_system_out, Time + "---time difference" + time/1000/60 + "Minutes");
    XCAPPLICATION.BASE_LOG.I (xcconfig.tag_system_out, Curtime + "---current time" + Format (new Date (Curtime), format_long_cn_1)); XCAPPLICATION.BASE_LOG.I (xcconfig.tag_system_out, Charttime + "---charttime" + Format (new Date (charttime), Format_
 
    long_cn_1));
    if (Time < A * 1000 && time >= 0) {return "just"; else if (Time >=-*1000 && Time < 3600 * 1000) {return format (new Date (charttime), format
 
    _HH_MM); else if (Time >= 3600 * 24 *1 * 1000 && Time < 3600 * 2 * 1000) {return "Yesterday" + Format (new Date (Charttime), format_hh_mm); else if (Time >= 3600 * 2 * 1000 && Time < 3600 * 3 * 1000) {return "The day before yesterday" + Format (n
    EW Date (charttime), format_hh_mm); else if (Time >= 3600 * 3 * 1000 && Time < 3600 * 365 * 1 * 1000) {return format (new Da
    Te (charttime), format_mm_dd_hh_mm); else if (Time >= 3600 * 365 * 1 * 1000 && Time < 3600 * * 365 * 2 * 1000) {return "last year" +
    Format (new Date (Charttime), format_mm_dd_hh_mm); else if (Time >= 3600 * 365 * 2 * 1000 && Time < 3600 * * 365 * 3 * 1000) {return "the year before" +
    Format (new Date (Charttime), format_mm_dd_hh_mm);
    else if (Time >= 3600 * 365 * 3 * 1000) {return format (new Date (Charttime), format_long_cn_1);
    else {return "just";
 }
  }

Here is a small problem, that is, natural day time across the actual day time, there may be yesterday's time does not show yesterday, and shown as hh:mm, so the test came to the door, asked to change, will be 2 minutes-24 hours of conditions to 2 minutes-today.

So here's the need to change

* No display in 2 minutes
* 2 minutes-Today hh:mm
* Yesterday hh:mm yesterday
* The day before yesterday hh:mm
* This year MM:DD hh:mm
* Last year MM:DD hh:mm
* Mm:dd hh:mm years ago
* Farther Yyyy:MM:DD hh:mm

This is not much of a problem, the question is in the year of the situation, 2015-01-01 00:01.001 of the first three minutes to accept the message, that is, 2014-12-31 should be shown as yesterday or last year. If the information is received more than the time, how to display.

After some tearing force, finally finalized, here for the product again, the requirements of product Ligue Ah, as the ultimate version exists.

/** * Ultimate Method * Converts a timestamp to a prompt time string, such as * no display within 2 minutes * 2 minutes-today 2 minutes-today hh:mm * Yesterday hh:mm yesterday * hh:mm * this year
   M:DD hh:mm * Last year mm:dd hh:mm * MM:DD hh:mm * Further yyyy:MM:DD hh:mm * Millisecond calculation * @param time * @return */public static String convertwecharttimeformatfinalmethed (long) {Long curtime = System.currenttimemillis (
    ) ;
 
    String Showtimeformat = "";
    Long temp = curtime-time;
      if (Temp < 1000 && Temp >= 0) {Showtimeformat = "";
    return showtimeformat;
 
Date Maytime = new Date (time);
    Date today = Utildate.parse ("2015-01-01 02:02:02.001", utildate.format_full);
    Date today = new Date ();
    Time value String maytime_format_short = FORMAT (Maytime, Format_short);
 
    String maytime_format_short_year = getyear (Maytime);
 
    if (Maytime.after (today)) {//except Showtimeformat = Format (Maytime, format_long_cn_1); } else {if (Maytime_format_short!= null &&amP!maytime_format_short.trim (). toString (). Equals ("")) {//Today's time yyyy-mm-dd String today_str = FORMAT
        Format_short);
 
        String thisyear_str = getyear (today);
        Yesterday's time yyyy-mm-dd Calendar callastday = Calendar.getinstance ();
        Callastday.settime (today);
        Callastday.add (Calendar.day_of_year,-1);
        System.out.println ("Yesterday:" + Format (callastday.gettime (), format_short));
 
        String lastday = Format (Callastday.gettime (), format_short);
        Time the day before yesterday yyyy-mm-dd Calendar calpreviousday = Calendar.getinstance ();
        Calpreviousday.settime (today);
        Calpreviousday.add (Calendar.day_of_year,-2);
        System.out.println ("The day before yesterday:" + Format (calpreviousday.gettime (), format_short));
 
        String previousday = Format (Calpreviousday.gettime (), format_short);
        Last year's time yyyy Calendar callastyear = calendar.getinstance ();
        Callastyear.settime (today);
Callastyear.add (Calendar.year,-1);        String lastyear = getyear (Callastyear.gettime ());
 
        System.out.println ("Last year:" + Format (callastyear.gettime (), format_short));
        The year before last yyyy Calendar calpreviousyear = calendar.getinstance ();
        Calpreviousyear.settime (today);
        Calpreviousyear.add (Calendar.year,-2);
        String previousyear = getyear (Calpreviousyear.gettime ());
 
        System.out.println ("The previous year:" + Format (calpreviousyear.gettime (), format_short)); First to determine if it is today if (Maytime_format_short.equals (TODAY_STR)) {//Today, then display as 13:12 Showtimeformat = forma
        T (Maytime, FORMAT_HH_MM); else if (maytime_format_short.equals (Lastday)) {//Yesterday Showtimeformat = "Yesterday" + FORMAT (maytime,format_h
 
        H_MM); else if (maytime_format_short.equals (Previousday)) {//Yesterday Showtimeformat = "day before Yesterday" + FORMAT (maytime,form
 
        AT_HH_MM); else if (maytime_format_short_year.equals (THISYEAR_STR)) {//this year ShowTimeFormat = Format (Maytime, format_mm_dd_hh_mm); else if (maytime_format_short_year.equals (lastyear)) {//Last Showtimeformat = ' last year ' + FORMAT (Maytime, F
        ORMAT_MM_DD_HH_MM); else if (maytime_format_short_year.equals (previousyear)) {//the year before Showtimeformat = "year ago" + FORMAT (maytim
        E, format_mm_dd_hh_mm);
        else {//besides Showtimeformat = Format (Maytime, format_long_cn_1);
  }} return Showtimeformat;

 }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.