Android Time Format getTimeString(String dateString)

Here you can find the source of getTimeString(String dateString)

Description

get user friendly data string, a combination of getCalenderFromDateString and getUserFriendlyTime

Parameter

Parameter Description
dateString a parameter

Return

converted user-friendly data string, null if parse fail

Declaration

public static String getTimeString(String dateString) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**/*  w ww.java2 s. c om*/
     * H: 00-23, h: 0-11(am/pm) Z: timezone (+8000) see full doc <a href=
     * "http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html"
     * >docs.oracle.com</a>. Locale.US is always available on all platform (see
     * Locale javadoc)
     */

    private static final java.text.DateFormat weiboCreateDateFormat = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
    private static final java.text.DateFormat hhmmDateFormat = new SimpleDateFormat(
            "HH:mm", Locale.US);
    private static final java.text.DateFormat mMddHHmmDateFormat = new SimpleDateFormat(
            "M?d??? HH:mm", Locale.US);

    /**
     * get user friendly data string, a combination of getCalenderFromDateString
     * and getUserFriendlyTime
     * 
     * @param dateString
     * @return converted user-friendly data string, null if parse fail
     */
    public static String getTimeString(String dateString) {
        return getUserFriendlyTime(getCalenderFromDateString(dateString));
    }

    public static String getUserFriendlyTime(Calendar date) {
        if (date == null) {
            return null;
        }
        Calendar now = Calendar.getInstance();
        long interval = (now.getTimeInMillis() - date.getTimeInMillis()) / 1000;
        String result = null;
        if (interval <= 60 * 60 && interval >= 0) {
            if (interval < 6) {
                // 0 - 5 seconds
                result = "??";
            } else if (interval < 60) {
                // 7 seconds - 1 minutes
                result = String.valueOf(interval) + "???";
            } else {
                // 1 minutes - 1 hour
                result = String.valueOf(interval / 60) + "????";
            }
        } else {
            // older than one hour
            Calendar yesterDay = Calendar.getInstance();
            yesterDay.setTime(now.getTime());
            yesterDay.add(Calendar.DAY_OF_YEAR, -1); // also work for date like
            // January 1st.
            if (date.get(Calendar.YEAR) == now.get(Calendar.YEAR)
                    && date.get(Calendar.DAY_OF_YEAR) == now
                            .get(Calendar.DAY_OF_YEAR)) {
                // today
                result = hhmmDateFormat.format(date.getTime());
            } else if (date.get(Calendar.YEAR) == yesterDay
                    .get(Calendar.YEAR)
                    && date.get(Calendar.DAY_OF_YEAR) == yesterDay
                            .get(Calendar.DAY_OF_YEAR)) {
                // yesterday
                result = "??" + hhmmDateFormat.format(date.getTime());
            } else {
                // otherwise
                result = mMddHHmmDateFormat.format(date.getTime());
            }
        }
        return result;
    }

    /**
     * parse Date string like: "Sat Sep 21 23:39:40 +0800 2013" to
     * java.util.Date object
     * 
     * @param dateString
     * @return Calendar representation or epoch if parse error
     */
    public static Calendar getCalenderFromDateString(String dateString) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(weiboCreateDateFormat.parse(dateString));
        } catch (ParseException e) {
            cal.setTime(new Date(0));
        }
        return cal;
    }
}

Related

  1. toModifiedTimeString(Date modified)
  2. convertDateTime(String date)
  3. convertDateTime(long date)
  4. getTimeStr(long time)
  5. getTimeString(Date utcDate)
  6. getTimeString(String utcTimeString)
  7. dateTimeToXSDate(Date date)
  8. dateTimeToXSDateTime(Date date)