4

I need to convert Persian (Shamsi) date to Gregorian date. I search alot and find different solution. The most reliable answer seems to be ibm library; icu4j. So, I start to play with it in order to change date, but the presented examples do not work for me. When I set Persian date in calendar and get timestamp, it would be -18009228344000 for 1399/04/16. But when I use Gregorian calendar the time stamp for 2020/07/06 is : 1593977400000. So, I find the way! I find the difference and add it to Persian timestamp. Now I can convert any Persian date to Gregorian. But I wonder if this solution is reliable or not. Here is the code:

ULocale locale = new ULocale("@calendar=persian");
Calendar persianCalendar = Calendar.getInstance(locale);
persianCalendar.clear();
persianCalendar.set(1399, 3, 16); 
long persianTime = persianCalendar.getTimeInMillis();
long GeorgeanTime =persianTime + 19603205744000L;
persianCalendar.setTimeInMillis(GeorgeanTime);
Date gregorianDate = persianCalendar.getTime();

The output is Mon Jul 06 00:00:00 IRDT 2020 which is the correct conversion of Persian date.

Narges
  • 1,345
  • 6
  • 14
  • 29
  • Is this helpful [https://stackoverflow.com/questions/24968568/is-there-any-library-or-algorithm-to-convert-shamsijalali-to-gregorian-date-in] – DuncG Jul 06 '20 at 18:33
  • I try to use a reliable library; which I think icu4j is. But I couldn't use it correctly. I need to get find the right solution with icu4j. Would you please help me to fix it? – Narges Jul 07 '20 at 06:42
  • @DuncG Your answer was helpful I guess. Just some thing strange happened. Today The timestamp of Persian calendar was 1593977400000. So, the above algorithm was awfully useless. Am I crazy or what?! – Narges Jul 07 '20 at 07:30
  • I deleted my answer as I had used wrong jar, I will have another check. – DuncG Jul 07 '20 at 12:08

0 Answers0