see2851
see2851

Reputation: 657

Why SimpleDateFormat in Android does not work?

public static String getDateFormatStr(long time,String formatStr){
    String timeStr=null;
    SimpleDateFormat setDateFormat = new SimpleDateFormat(formatStr);
    timeStr = setDateFormat.format(time);
    return timeStr;
}

e.g : HH:mm, E, MMM dd, yyyy

result: 11:30, 5,11 08, 2012

Question: Why is not 11:30,Web,Nov 08,2012,, but 11:30, 5, 11 08, 2012?

Upvotes: 1

Views: 274

Answers (3)

Jules
Jules

Reputation: 15199

As a quick guess, your phone is set to use an unusual Locale. Try specifying a specific Locale when you create your SimpleDateFormat, i.e. new SimpleDateFormat(formatStr, Locale.ENGLISH)

Upvotes: 0

mohammed momn
mohammed momn

Reputation: 3210

Try to use the Calendar class and this sample:

Calendar cal=Calendar.getInstance(); `
SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM");
String month_name = month_date.format(cal.getTime());

Month name will contain the full month name. If you want short month name use this:

SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());

Upvotes: 0

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

I tested your code. its simply working good. check this testing

 public static void main(String[] args) {

    long l = new Date().getTime();
    System.out.println(getDateFormatStr(l,"HH:mm, E, MMM dd, yyyy"));


}

public static String getDateFormatStr(long time,String formatStr){
    String timeStr=null;
    SimpleDateFormat setDateFormat = new SimpleDateFormat(formatStr);
    timeStr = setDateFormat.format(time);
    return timeStr;
}

Result: 08:49, Mon, Nov 12, 2012

Upvotes: 1

Related Questions