Reputation: 57
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 /١٧/٢٠٢١.
Upvotes: 1
Views: 938
Reputation: 11
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());
Upvotes: 1
Reputation: 79085
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.
Upvotes: 5
Reputation: 86276
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.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 3