Reputation: 119
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
Upvotes: 2
Views: 2595
Reputation: 44071
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);
If the input contains a time part then the whole thing is rather a timestamp and not a calendar date. So users can parse to a moment instead.
TZID tzid = () -> "Asia/Tehran"; // used as default time zone for parsing
ChronoFormatter<Moment> f =
ChronoFormatter.setUpWithOverride(Locale.ENGLISH, PersianCalendar.axis())
.addPattern("yyyy/MM/dd HH:mm[:ss]", PatternType.CLDR) // optional seconds
.build()
.withTimezone(tzid);
Moment expected =
PlainTimestamp.of(2018, 4, 3, 10, 15, 5).inTimezone(tzid);
assertThat(
f.parse("1397/01/14 10:15:05"),
is(expected));
The parsed moment can be converted back to Persian calendar this way:
assertThat(
expected.toGeneralTimestamp(PersianCalendar.axis(), tzid, StartOfDay.MIDNIGHT).toDate(),
is(PersianCalendar.of(1397, 1, 14)));
Upvotes: 4
Reputation: 339679
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.
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
.
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.
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.
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?
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.
Upvotes: 2
Reputation: 314
Try to use SimpleDateFormatter as follows:
DateFormat df= new SimpleDateFormat( "YYYY/MM/dd" );
String formattedDate = df.format(dt);
Upvotes: -1