I am using the class shown in this Question to convert Jalali (shamsi) date to Georgian.
And with the below command, I turn the Hijri date into Georgian
Date dt = JalaliCalendar.getGregorianDate("1397/01/14");
Toast.makeText(getApplicationContext(), dt.toString(), Toast.LENGTH_LONG).show();
I want to display the result as follows : 2018/04/03
Caveat: I know nothing about either Jalali calendar nor Hijri calendar except for two minutes spent scanning the Wikipedia pages. But I might be able to help you get part way to a solution.
java.time
You are using troublesome old date-time classes that were supplanted years ago by the java.time classes.
The java.time classes come bundled with a HijrahChronology
implementation of Chronology
.
Suggestion: Implement java.time.chrono.Chronology
I do not see any implementation of Chronology
for the Jalali calendar either bundled with java.time nor in the ThreeTen-Extra project that does provide other chronologies.
I imagine it might be ideal if someone did contribute such an implementation. Perhaps such an implementation could be written by following the example of the HijrahChronology
bundled with Java.
HijrahChronology.java
HijrahDate.java
HijrahEra.java
hijrah-config-islamic-umalqura.properties
Hijri (not Jalali)
Here is my ignorant attempt at using the HijrahDate
class.
LocalDate ld = LocalDate.of( 2018, Month.APRIL , 3 ) ; // Western calendar, defined in `IsoChronology` class: https://docs.oracle.com/javase/10/docs/api/java/time/chrono/IsoChronology.html
HijrahDate hd = HijrahDate.from( ld ); // Using class java.time.chrono.HijrahChronology https://docs.oracle.com/javase/10/docs/api/java/time/chrono/HijrahChronology.html
ld.toString(): 2018-04-03
hd.toString(): Hijrah-umalqura AH 1439-07-17
That seems to be correct for Hijri Calendar as shown on this Iranian Calendar Converter page.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Using my library Time4J (or Time4A on Android) enables following short solution out of the box:
String input = "1397/01/14";
PersianCalendar jalali =
ChronoFormatter
.ofPattern("yyyy/MM/dd", PatternType.CLDR, Locale.ROOT, PersianCalendar.axis())
.parse(input);
PlainDate gregorian = jalali.transform(PlainDate.axis());
String iso8601 = Iso8601Format.EXTENDED_DATE.format(gregorian);
System.out.println(iso8601); // 2018-04-03
No need to implement your own calendar chronology (which is generally a complex task). If you want to get the result with slashes instead of ISO-format then just use another formatter by:
String output = ChronoFormatter
.ofPattern("yyyy/MM/dd", PatternType.CLDR, Locale.ROOT, PlainDate.axis())
.format(gregorian);
Try to use SimpleDateFormatter as follows:
DateFormat df= new SimpleDateFormat( "YYYY/MM/dd" );
String formattedDate = df.format(dt);