Convert Jalali calendar to Georgian in java

2019-07-23 22:28发布

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

3条回答
Emotional °昔
2楼-- · 2019-07-23 22:39

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.

查看更多
迷人小祖宗
3楼-- · 2019-07-23 22:44

Try to use SimpleDateFormatter as follows:

    DateFormat df= new SimpleDateFormat( "YYYY/MM/dd" );
    String formattedDate = df.format(dt);
查看更多
啃猪蹄的小仙女
4楼-- · 2019-07-23 22:57

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);
查看更多
登录 后发表回答