0

I am trying to format the date using the below code.

2021-01-02 returns JANUARY 2020 in one device and JANUARY 2021 on another device. Why is it so?

formatDate(transactionItem.dateLabel, "yyyy-MM-dd", "MMMM YYYY")?.toUpperCase()


public static String formatDate(String inputDate, String inputFormat, String outputFormat) {
        try {
            Locale appLocale = new Locale(LocaleHelper.getDefaultLanguage());
            DateFormat originalFormat = new SimpleDateFormat(inputFormat, appLocale);
            DateFormat targetFormat = new SimpleDateFormat(outputFormat);
            Date dateObject = originalFormat.parse(inputDate);
            String formattedDate = targetFormat.format(dateObject);
            return formattedDate;
        } catch (ParseException var9) {
            return "";
        } catch (Exception var10) {
            return "";
        }
    }
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Ankush Kapoor
  • 445
  • 1
  • 8
  • 20
  • what are the locales set on the devices? – Stachu Jan 02 '21 at 07:37
  • @Stachu appLocale returns en for both devices – Ankush Kapoor Jan 02 '21 at 07:38
  • 2
    "YYYY" is week year, which is different from calendar year (see https://stackoverflow.com/questions/34691582/how-does-java-week-year-really-work). I don't know why it is different for two devices, but possible causes are different locales (causing different calendars to be used), or Android API level (week year "YYYY" is introduced in API 24 according to https://developer.android.com/reference/java/text/SimpleDateFormat) – Maurice Lam Jan 02 '21 at 08:04
  • Does this answer your question? [How does Java "week year" really work?](https://stackoverflow.com/questions/34691582/how-does-java-week-year-really-work) – Ole V.V. Jan 07 '21 at 05:18
  • Does this answer your question? [Y returns 2012 while y returns 2011 in SimpleDateFormat](https://stackoverflow.com/questions/8686331/y-returns-2012-while-y-returns-2011-in-simpledateformat) – Ole V.V. Jan 07 '21 at 05:19
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 07 '21 at 05:19

1 Answers1

0

There are two major and related problems in your code:

  1. Using SimpleDateFormat without Locale: You have used new SimpleDateFormat(outputFormat) without a Locale and as such, it is error-prone. Check this answer to learn more about the problem that may occur due to lack of Locale. Since your expected output is in English, use the English type of Locale e.g. new SimpleDateFormat(outputFormat, Locale.ENGLISH).
  2. Y is used for Week year and for SimpleDateFormat, it is Locale-sensitive i.e. it may have different values for different locales. Check this discussion to learn more about it. From your question, it looks like you mean Year and not Week year and therefore, you should use y as already specified in your inputFormat.

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

Learn about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110