Returns the date formatted appropriately for display in the listview - Android User Interface

Android examples for User Interface:ListView

Description

Returns the date formatted appropriately for display in the listview

Demo Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;
import android.util.Log;

public class Main {
    public static final String DateFormat = "EEE, d MMM yyyy HH:mm:ss";

    /**//w  w  w . ja va 2 s.c  o m
     * Returns the date formatted appropriately for display in the list view
     * @param date
     * @return
     */
    public static String convertDate(String srcDate, String srcDateFormat,
            String destDateFormat) {
        DateFormat srcFormat = new SimpleDateFormat(srcDateFormat,
                Locale.US);

        String friendlyDate = srcDate;
        try {
            Date src = srcFormat.parse(srcDate);
            DateFormat dstFormat = new SimpleDateFormat(destDateFormat,
                    Locale.US);
            friendlyDate = dstFormat.format(src);
        } catch (ParseException e) {
            Log.e("convertDate",
                    "Caught ParseException on date " + e.getMessage());
        }
        return friendlyDate;
    }
}

Related Tutorials