I get wrong output in ICU4J library

582 Views Asked by At

I want to convert date from Persian calendar to Gregorian Calendar. For this, I use ICU4J library (version 65.1). The problem is that this library gives wrong output for some dates.

Here is my code:

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

GregorianCalendar gregoriancal = new GregorianCalendar();

Calendar persiancal = Calendar.getInstance(locale);
//            year month day
persiancal.set(1398, 11, 16);

gregoriancal.setTime(persiancal.getTime());

String day = gregoriancal.get(Calendar.DATE) + "";

System.out.println(day);
----------------------------------------
output: 6

this date in persian calendar ( 1398/11/16 ) equls to 2020-02-05 Wednesday February in Gregorian Calendar

but it gives me 6 as output ( while it should give 5 )

is there something wrong with my code that results in a wrong output??

1

There are 1 best solutions below

0
On

After looking at the javadoc passing the ULocale should do the trick to put the GregorianCalendar in the correct time zone:

com.ibm.icu.util.GregorianCalendar gregoriancal =
    new com.ibm.icu.util.GregorianCalendar(locale);

I think the month may be 0-based as in java.util.Calendar.

    persiancal.set(1398, 11-1, 16);