Good date converter for Jalali Calendar in Java?

I am developing a Java application and I have timeStamp ( 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 a date on the Jalali calendar. I searched, but did not find any good library. Do you know a reliable and good library for converting (or creating dates in timeStamp format from timeStamp )? I do not need an implementation or algorithm, because this problem is too wrong and has many rules, I need a reliable solution

+38
java date localization calendar jalali-calendar
Apr 30 '14 at 10:16
source share
6 answers

Take a look at this: https://github.com/amirmehdizadeh/JalaliCalendar

The code looks good, a maven-based project, a lot of unit tests.

+13
Apr 30 '14 at 10:21
source share

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

The APIs are similar to the standard Java APIs, but add additional support for localization and internationalization (for example, time and calendar issues, sorting, formatting rules, and regular expression implementations with proper Unicode support).

To create a Persian calendar and display a formatted date in Farsi, you would, for example, 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 lead to the conclusion:

چهارشنبه 10 اردیبهشت 1393 ه.ش.

+66
Apr 30 '14 at 10:48
source share

I developed my own Persian (jalali) calendar in Java in my Time4J library. The implementation deploys the Borkovsky algorithm (valid until at least the 21st year - 2129 is not a 2025 error).

The solution to a specific problem of OP:

 // conversion from jalali to gregorian by constructed input PersianCalendar jalali = PersianCalendar.of(1394, 11, 5); // or use a safe enum instead of the month number: // PersianCalendar jalali = PersianCalendar.of(1394, PersianMonth.BAHMAN, 5); PlainDate gregorian = jalali.transform(PlainDate.class); System.out.println(gregorian); // 2016-01-25 // conversion to millis-since-unix (timezone-dependent) Moment moment1 = gregorian.atStartOfDay().inTimezone(ASIA.TEHRAN); long millisSinceUnix = TemporalType.MILLIS_SINCE_UNIX.from(moment1); System.out.println(millisSinceUnix); // 1453667400000L // conversion of millis-since-unix to jalali (timezone-dependent) Moment moment2 = TemporalType.MILLIS_SINCE_UNIX.translate(millisSinceUnix); PlainDate gregorian2 = moment2.toZonalTimestamp(ASIA.TEHRAN).toDate(); System.out.println(gregorian2.transform(PersianCalendar.class)); // AP-1394-11-05 // formatting and parsing in Farsi language using full style ChronoFormatter<PersianCalendar> f1 = ChronoFormatter.ofStyle(DisplayMode.FULL, new Locale("fa"), PersianCalendar.axis()); System.out.println(f1.format(jalali)); // ه‍.ش. ۱۳۹۴ بهمن ۵, دوشنبه System.out.println(f1.parse("ه‍.ش. ۱۳۹۴ بهمن ۵, دوشنبه")); // AP-1394-11-05 // formatting in English language using custom pattern ChronoFormatter<PersianCalendar> f2 = ChronoFormatter.ofPattern( "d. MMMM yyyy", PatternType.CLDR, Locale.ENGLISH, PersianCalendar.axis()); System.out.println(f2.format(jalali)); // 5. Bahman 1394 

Of course, the calendar offers more options, such as arithmetic of dates (adding days or months, calculating delta in days, months, etc.) or manipulating a field / element (simple transition to the last day of the month, etc.).

Side notes about other libraries offered so far here:

The amirmehdizadeh / JalaliCalendar and ICU4J libraries use zero months . This can be very confusing. A non-intuitive example using the amirmehdizadeh library:

 YearMonthDate jalali = new YearMonthDate(1394, 10, 5); // really month 11 (Bahman) YearMonthDate gregorian = JalaliCalendar.jalaliToGregorian(jalali); System.out.println(gregorian); // 2016/0/25 => really month 1 (January) 

As for internationalization , I don’t think that ICU4J offers more than Time4J in the field of the Persian calendar, since the latter is based on the latest version of the CLDR version of v28. Time4J actually supports about 25 languages ​​for the Persian months and eras (including Farsi and Pashto).

+6
Feb 09 '16 at 13:55
source share

There is a good library on github that has a very simple API and has many features, and is also available on mavenCentral:

https://github.com/razeghi71/JalaliCalendar

+5
Apr 21 '16 at 10:08
source share

You can use JalCal that the Jalali (Persian) Calender Convertor in Java:

 <dependency> <groupId>com.github.sbahmani</groupId> <artifactId>jalcal</artifactId> <version>1.0</version> </dependency> 

1. Jalali - Gregorian

  Calendar expected4 = Calendar.getInstance(TimeZone.getDefault()); expected4.set(2014, 7, 5, 1, 23, 1); assertThat(JalCal.jalaliToGregorian(1393, 5, 14, 1, 23, 1).toString()).isEqualTo(expected4.getTime().toString()); 

2. Gregorian - Jalali

  Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 5); cal.set(Calendar.MONTH, 6); cal.set(Calendar.YEAR, 2014); cal.set(Calendar.HOUR_OF_DAY, 10); cal.set(Calendar.MINUTE, 25); cal.set(Calendar.SECOND, 1); cal.set(Calendar.MILLISECOND, 0); assertThat(JalCal.gregorianToJalali(cal.getTime(), true)).isEqualTo("14/04/1393 10:25:01"); assertThat(JalCal.gregorianToJalali(new Date(1426883400000l), true)).isEqualTo("01/01/1394 00:00:00"); 
+2
Aug 29 '17 at 10:09 on
source share

You can find a Java-based Jalali graphic calendar called persain-calendar . Here is a screenshot of the Persian calendar.

Persian calendar

-2
Jul 17 '15 at 16:57
source share



All Articles