A good date converter for Jalali Calendar in Java?

Saeed picture Saeed · Apr 30, 2014 · Viewed 23.5k times · Source

I'm developing a Java App and I have a timeStamp (in long). I can easily use this code to change it to a Gregorian date:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);

But I need to have the date in Jalali Calendar. I searched but didn't found any good library. Do you know a reliable and good library for converting (or creating dates in Jalali format from timeStamp)? I don't need an implementation or an algorithm, cause this issue is too buggy and has a lot of rules, I need a reliable solution

Answer

jarnbjo picture jarnbjo · Apr 30, 2014

For better localization and language support, it is often convenient to use the ICU (International Components for Unicode) library from IBM.

The APIs are similar to the standard Java APIs, but add additional support for localization and internationalization (e.g. time and calendar issues, sorting, formatting rules and a regex implementation with proper Unicode support).

To create a Persian calendar and output the formatted date in Farsi, you would e.g. do something like this:

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ULocale;

...

ULocale locale = new ULocale("fa_IR@calendar=persian");

Calendar calendar = Calendar.getInstance(locale);
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);

System.out.println(df.format(calendar));

This will output:

چهارشنبه ۱۰ اردیبهشت ۱۳۹۳ ه‍.ش.