Gets a human-readable long time string (includes both the time and date, always). - Android java.util

Android examples for java.util:Time

Description

Gets a human-readable long time string (includes both the time and date, always).

Demo Code


//package com.java2s;
import android.content.Context;
import android.text.format.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static String toStringLong(Context context, Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);/*  www .  j a va  2  s .c o m*/
        return toStringLong(context, cal);
    }

    /**
     * Gets a human-readable long time string (includes both the time and date, always).
     */
    public static String toStringLong(Context context, Calendar date) {
        Calendar now = Calendar.getInstance();
        int hourInt;
        final boolean is24 = DateFormat.is24HourFormat(context);
        if (is24)
            hourInt = date.get(Calendar.HOUR_OF_DAY);
        else
            hourInt = date.get(Calendar.HOUR);
        int minuteInt = date.get(Calendar.MINUTE);
        String dayStr = getNumberWithSuffix(date.get(Calendar.DAY_OF_MONTH));

        String timeStr = "";
        if (hourInt == 0)
            timeStr += "12";
        else
            timeStr += "" + hourInt;
        if (minuteInt < 10)
            timeStr += ":0" + minuteInt;
        else
            timeStr += ":" + minuteInt;
        if (!is24) {
            if (date.get(Calendar.AM_PM) == Calendar.AM)
                timeStr += "AM";
            else
                timeStr += "PM";
        }

        if (now.get(Calendar.YEAR) == date.get(Calendar.YEAR)) {
            // Same year
            return timeStr + " "
                    + convertMonth(date.get(Calendar.MONTH), false) + " "
                    + dayStr;
        } else {
            // Different year
            return timeStr + " "
                    + convertMonth(date.get(Calendar.MONTH), false) + " "
                    + dayStr + ", " + date.get(Calendar.YEAR);
        }
    }

    private static String getNumberWithSuffix(int number) {
        int j = number % 10;
        if (j == 1 && number != 11) {
            return number + "st";
        }
        if (j == 2 && number != 12) {
            return number + "nd";
        }
        if (j == 3 && number != 13) {
            return number + "rd";
        }
        return number + "th";
    }

    private static String convertMonth(int month, boolean useShort) {
        String monthStr;
        switch (month) {
        default:
            monthStr = "January";
            break;
        case Calendar.FEBRUARY:
            monthStr = "February";
            break;
        case Calendar.MARCH:
            monthStr = "March";
            break;
        case Calendar.APRIL:
            monthStr = "April";
            break;
        case Calendar.MAY:
            monthStr = "May";
            break;
        case Calendar.JUNE:
            monthStr = "June";
            break;
        case Calendar.JULY:
            monthStr = "July";
            break;
        case Calendar.AUGUST:
            monthStr = "August";
            break;
        case Calendar.SEPTEMBER:
            monthStr = "September";
            break;
        case Calendar.OCTOBER:
            monthStr = "October";
            break;
        case Calendar.NOVEMBER:
            monthStr = "November";
            break;
        case Calendar.DECEMBER:
            monthStr = "December";
            break;
        }
        if (useShort)
            monthStr = monthStr.substring(0, 3);
        return monthStr;
    }
}

Related Tutorials