0

I have date String in the following format - eg.:

Thu, 17 Mar 2016 19:30:25 +0000
Sun, 06 Mar 2016 12:43:13 +0000

I want to convert this date into a more readable format:

Thu, 17 Mar 2016
Sun, 06 Mar 2016


public static String getMoreReadableDateFormat(String dateStringToConvert) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
    Date convertedDate;
    try {
        convertedDate = dateFormat.parse(dateStringToConvert);
    } catch (ParseException e) {
        // could not convert date, return the initial form
        return dateStringToConvert;
    }
    String formattedDate = new SimpleDateFormat("E, dd MMM yyyy").format(convertedDate);
    return formattedDate;
}

Strange is that this code works fine for me (I get the simplified date version), but for some other peoples from other countries is not working and cannot convert date string into the simplified version. I know it has to be related to Locale, but don't know how to fix this.

Paul
  • 3,812
  • 10
  • 50
  • 73

2 Answers2

0

You can try to set your phone's date format to french, or something else to reproduce the behavior you mentioned.

Are you sure that dateStringToConvert parameter is always in a correct format ?

I would suggest you change the first line to:

SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Michel
  • 56
  • 4
  • `dateStringToConvert` is always in the correct format. Seems that if I use `Locale.ENGLISH` is working, but I'm not sure if is correct to use english all the time. If I use this Locale: Locale current = getResources().getConfiguration().locale; - it doesn't work in some countries also. – Paul Mar 17 '16 at 20:24
0

It's important to note here the difference between DateFormats like Date, Time, and Date and Time as listed in the link below. Given your block of code, it looks like you expect a Date and Time string to be passed in and you expect to return just a Date. I'm guessing that it's not following the format that you declare as

new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z")

Also, it doesn't look like that string matches the Predefined Formats. You can try reformatting it to use

DateFormat dateAndTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, currentLocale);

This would accept a string like

"Tuesday, June 30, 2009 7:03:47 AM PDT"

parse the parameter using the formatter above and then convert a different dateFormatter

DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);

you can get the default local from

Locale.getDefault()

So your code would look something like this

public static String convertDate(String dateStringToConvert) throws ParseException {
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());
        Date convertedDate = dateFormat.parse(dateStringToConvert);
        return DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault()).format(convertedDate);
    }

Check here for more information related to using predefined formats. https://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html

jcarey
  • 1
  • 2
  • `Locale.getDefault()` doesn not seems to work in each country. I used multiple formats: "E, dd MMM yyyy HH:mm:ss Z" and "EEE, d MMM yyyy HH:mm:ss Z" - but both had the same result: worked for me, but not in some others countries. Your method doesn't work for me either.. – Paul Mar 17 '16 at 21:51
  • Are you sure your Timezone is set properly? `+0000` should be something like `EST` and that could be throwing the exception while parsing – jcarey Mar 17 '16 at 23:45