Reputation: 1355
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.
Upvotes: 4
Views: 316