1

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • About terminology: You say "Hijri" and probably mean "Solar Hijri" (= Jalali), and then you accept an answer of B. Bourke who understands "Hijri" as the lunar Hijri calendar (of Saudi-Arabia?!) and gives you no solution at all but just says: "Implement it yourself". IMHO a total misunderstanding in the communication. – Meno Hochschild Apr 05 '18 at 10:15

3 Answers3

3

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);

Update for question in comment:

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)));
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
2

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.

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.

screenshot of Iranian calendar converter for 2018-04-03 converting to both Jalali & Hijri


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?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Try to use SimpleDateFormatter as follows:

    DateFormat df= new SimpleDateFormat( "YYYY/MM/dd" );
    String formattedDate = df.format(dt);
  • 1
    Formatting patterns are case-sensitive. `YYYY` uppercase means week-based year rather than calendar-year. – Basil Bourque Apr 03 '18 at 20:27
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Apart from that, the question was about conversion, and the answer hardly addresses that. – Ole V.V. Apr 04 '18 at 05:46
  • 1
    Leaving aside the wrong pattern, its main error is the fact that the Jalali calendar is not supported at all by `SimpleDateFormat`. – Meno Hochschild Apr 05 '18 at 09:46