Verify Hijri date on Java 6 (Islamic calendar)

I want to verify the Hijri date In my application, Hijri date will appear in DD / mm / yyyy format I want to verify that the date is valid in the joda time API

For example, I have a date:

String date = "30/02/1403";
String Pattern = "dd/MM/YYYY";

Can you help me verify Hijri's appointment?

My limitations are:

>I want to verify in Java 6 I can't use it in Java 8. > I have to verify the Islamic calendar Verify with Hijri date My date is already in Hijri format I must verify that the date is valid with the Islamic calendar. > In the Islamic calendar, 30 / 02 / 1403 is the effective date of the second month

Solution

java. time

java. Time, a modern Java date and time API, can do this:

DateTimeFormatter hijrahFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
                .withChronology(HijrahChronology.INSTANCE);
    try {
        HijrahDate date = hijrahFormatter.parse("30/02/1403",HijrahDate::from);
        System.out.println("A valid date: " + date);
    } catch (DateTimeParseException dtpe) {
        System.out.println("Not a valid date: " + dtpe.getMessage());
    }

The output of this code snippet is:

Format mode letters are case sensitive You need to use lowercase yyyy as calendar year (or uuuu as preleptic year)

Java 6? There are three options

It's out of date now (Java 11 is out) My suggestion is:

>Using threeten backup, Java Time backend to Java 6 and 7 It turns out that it doesn't apply to your situation, but you can. > Ask you to use joda time from the beginning. > Use time4j

The first two options will reject your date on 30 / 02 / 1403, but I believe there are only 29 days in the second month of 1403

ThreeTen Backport

DateTimeFormatter hijrahFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu")
            .withChronology(HijrahChronology.INSTANCE);
    String dateString = "30/02/1403";
    try {
        TemporalAccessor parsed = hijrahFormatter.parse(dateString);
        HijrahDate date = HijrahChronology.INSTANCE.date(parsed.get(ChronoField.YEAR),parsed.get(ChronoField.MONTH_OF_YEAR),parsed.get(ChronoField.DAY_OF_MONTH));
        if (date.format(hijrahFormatter).equals(dateString)) {
            System.out.println("A valid date: " + date);
        } else {
            System.out.println("Not a valid date: " + dateString);
        }
    } catch (DateTimeParseException dtpe) {
        System.out.println("Not a valid date: " + dtpe.getMessage());
    }

Output:

According to my understanding, the following simpler method of building dates should work, but not:

HijrahDate date = HijrahDate.from(parsed);

I got org threeten. bp. Datetimeexception: field not found: epochday The way I construct the date is loose, that is, it accepts that the date of the month is out of range and generates the date of Hijrah umalqura ah 1403-03-01 So to complete the validation, I formatted the date and asked me to get the original string again

If not 30 / 02 / 1403, I try 29 / 02 / 1403, I get

Jorda time

DateTimeFormatter islamicFormatter = DateTimeFormat.forPattern("dd/MM/yyyy")
            .withChronology(IslamicChronology.getInstance());
    try {
        LocalDate date = LocalDate.parse("30/02/1403",islamicFormatter);
        System.out.println("A valid date: " + date);
    } catch (IllegalArgumentException iae) {
        System.out.println("Not a valid date: " + iae.getMessage());
    }

Joda time also believes that this month has 29 days, which is very clear:

I got a string of 29 / 02 / 1403:

Joda time's Islamic chronology has four different leap year patterns I've tried these four They all produce 29 days in the second month of 1403 Sorry, I'm as close as I can

From joda time homepage:

Time4J

I recommend to you the knowledgeable answers of Meno Hochschild, the author of time4j In my opinion, time4j version 3.50 solves your problem according to your requirements and Java 6

But is that date valid?

You are more professional than me I seem to understand that variations of Hijri calendar exist According to your variant, 30 / 02 / 1403 is a fully valid date The version provided by alhabib Islamic network service is agreed with you: Safar 301403 is on the same day as December 16, 1982 in the Gregorian calendar On the other hand, according to searchtruth COM, Safar 1403 has only 29 days in the current month (Safar 291403 corresponds to Gregory on December 15, 1982, followed by Rabi al awwal 11403, corresponding to December 16)

link

>Oracle tutorial: date time explains how to use Java time. > Java specification request (JSR) 310, which first describes Java time. > ThreeTen Backport project,java. Time backend to Java 6 and 7 (threeten for jsr-310). > Threetenabp, Android version of threeten backup > joda time home > time4j – advanced date, time, Zone and interval library for Java > answer by Meno Hochschild (integrity only) > Islamic (Hijri) calendar year 1982 CE based on global crescent moon sighting probability on Arabic Islamic Web Services > Hijri Islamic calendar November 1982 – 1403 التقويم الهجري والميلادي on searchtruth. com

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>