2

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??

farhad
  • 105
  • 1
  • 9
  • Use a [GregorianCalendar](https://docs.oracle.com/javase/10/docs/api/java/util/GregorianCalendar.html) with the correct Iranian time zone. At time 00:00 such being one day off can easily happen. – Joop Eggen Feb 05 '20 at 13:40
  • @Joop Eggen, could you please write the code ? Thanks – farhad Feb 05 '20 at 17:39
  • @omid i need your help. please give me your email address or wats app number – farhad Feb 09 '20 at 17:07
  • Sorry farhad, I am switching firms end of february, and there is relatively much to do. But I'll look into the code a second time. – Joop Eggen Feb 10 '20 at 08:45
  • 1
    Tried out zero based months as in java.util.Calendar. see my answer. It fits, but please do some research. I have no time. – Joop Eggen Feb 10 '20 at 09:06
  • Hi. Do you solve your problem? https://stackoverflow.com/questions/62534443/noclassdeffounderror-com-ibm-icu-util-ulocale can you please help? – Narges Jun 23 '20 at 12:19

1 Answers1

1

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);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138