1

I download the user's date data and write this data to firebase. However, the date format will not work correctly when data is added from Arabic locations. How can I fix this?

Output:

"٢٠٢١-٠٤-١٧"

My Code:

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            String currentDateandTime = df.format(new Date());

I do not want to receive data in Arabic alphabet. I should take the data as 17/04/2021 under any condition. Not as /١٧/٢٠٢١.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
MustapTR
  • 57
  • 6

3 Answers3

5

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

Using modern date-time API:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfEnglish = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);

        DateTimeFormatter dtfArabic = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH)
                .localizedBy(new Locale("ar"));

        // Change the ZoneId as per your requirement e.g. ZoneId.of("Asia/Dubai")
        LocalDate date = LocalDate.now(ZoneId.systemDefault());

        System.out.println(date.format(dtfEnglish));
        System.out.println(date.format(dtfArabic));
    }
}

Output:

18/04/2021
١٨/٠٤/٢٠٢١

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


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

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

I recommend that you use java.time, the modern Java date and time API, for your date work. If I have understood that Firebase doesn’t provide a date data type, then I think that for storing you will want:

    String currentDateandTime = LocalDate.now(ZoneId.systemDefault()).toString();
    System.out.println(currentDateandTime);

Output when running just now in my time zone:

2021-04-17

The result is independent of locale. On retrieval the one-arg LocalDate.parse(CharSequence) will parse the same string back into a LocalDate.

Edit: The format you want, 2021-04-17, is an international standard known as ISO 8601, and I consider it wise of you to use it in your program too. I am exploiting the fact that LocalDate and the other classes of java.time produce ISO 8601 from their toString methods and parse the same format back in their one-arg parse methods. So when dealing with ISO 8601 (as a rule) we don’t need to specify a format nor a formatter neither for formatting nor for parsing. Had you wanted a different format, you would have used a DateTimeFormatter for specifying it.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I don't know how to set the date format I want with this Example : yyyy-MM-dd – MustapTR Apr 18 '21 at 06:23
  • @MustapTR Sorry, that wasn’t clear. I have edited, I hope it helps. The trick is (perhaps surprising) that we don’t set the format. yyyy-MM-dd is the format that we get when we don’t set any. – Ole V.V. Apr 18 '21 at 06:30
0

If you want to take the data as 17/04/2021 under any condition, you could just change the second argument to be Locale.US .

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String currentDateandTime = df.format(new Date());
  • 2
    Correct, but as said in the other answers one should not want to use `SimpleDateFormat` in the 2020s. – Ole V.V. Apr 27 '23 at 08:53
  • 1
    The poorly-designed `Date` & `SimpleDateFormat` classes were years ago supplanted by the modern *java.time* classes defined in JSR 310. – Basil Bourque Apr 27 '23 at 18:42