Mohammed Ali
Mohammed Ali

Reputation: 2926

SimpleDateFormat warning To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(),

Do i need to be worried about this warning? What if I ignore the warning?
What does this warning mean:
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
In the 2nd Line of the code below. The App is working fine with the code. I wanted to show date eg, 19 Nov 2014.

public static String getFormattedDate(long calendarTimeInMilliseconds) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy");  //ON THIS LINE
    Date now = new Date();
    now.setTime(calendarTimeInMilliseconds);
    String strDate = sdfDate.format(now);
    return strDate;
}

I think this is a correct way to format date as shown here.

Upvotes: 55

Views: 28392

Answers (4)

Mr. Techie
Mr. Techie

Reputation: 886

We can get rid of that by using the following code:

val dateFormat = SimpleDateFormat("d MMM yyyy", Locale("IN"))

Although we have other solutions mentioned here, I wasn't getting Indian locale from here. After some researching, I figured out this solution.
You can find strings (for your country) to pass in Locale constructor from here, under Type: region

Upvotes: 0

kuzdu
kuzdu

Reputation: 7524

Same for kotlin

        //example long 1372339860 
        fun getDateTime(unixSeconds: Long): String {
         val date = java.util.Date(unixSeconds * 1000L)

         val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.getDefault())
         return simpleDateFormat.format(date)
        }

Call like this getDateTime(1372339860)

It will return 27.6.2013 15:31:00 GMT +2 The GMT+2 because I'm in Germany and we have summer time. You can use Locale.US as well.

Upvotes: 4

Valters Jansons
Valters Jansons

Reputation: 3962

You are currently using the SimpleDateFormat(String) constructor. This implies the default locale and as the Locale documentation tells you, be wary of the default locale as unexpected output can be produced on various systems.

You should instead use the SimpleDateFormat(String, Locale) constructor. It is going to take in an additional parameter - the locale you want to use. If you want to make sure the output is machine-readable in a consistent way (always looks the same, regardless of the actual locale of the user), you can pick Locale.US. If you do not care about machine redability, you can explicitly set it to use Locale.getDefault().

Using those on your example code would look something like this:

// for US
SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy", Locale.US);

// or for default
SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy",
        Locale.getDefault());

Upvotes: 91

JstnPwll
JstnPwll

Reputation: 8695

You can ignore it, but the warning is there to let you know the dates may display unexpectedly for users using a different language. If you want to force the format in your own locale, use Locale.US (or whichever locale matches your format requirements). Otherwise use one of the getInstance() methods for localized formatting.

Upvotes: 6

Related Questions