Validation of Hijri Date (Islamic Calendar) on Java 6












3














I want to validate Hijri date. In my application Hijri date will come as in the format of dd/MM/YYYY. I want to validate whether date is valid or not in Joda Time API.



For example, I have the date:



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


Can you please help me to validate Hijri date.



My restrictions are:




  • I want to validate in Java 6. I can't use in Java 8.

  • I have to validate with Islamic calendar. validate with Hijri date. My date is already in Hijri format. I have to validate that date is valid or not with Islamic calendar.

  • In the Islamic calendar 30/02/1403 is a valid date of 2nd month.










share|improve this question




















  • 2




    Java 8 does support Islamic calendar. HijrahChronology
    – Tridev Chaudhary
    Nov 13 '18 at 9:03










  • Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
    – Ole V.V.
    Nov 13 '18 at 10:26










  • But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
    – Rameez Kandhar
    Nov 13 '18 at 10:58
















3














I want to validate Hijri date. In my application Hijri date will come as in the format of dd/MM/YYYY. I want to validate whether date is valid or not in Joda Time API.



For example, I have the date:



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


Can you please help me to validate Hijri date.



My restrictions are:




  • I want to validate in Java 6. I can't use in Java 8.

  • I have to validate with Islamic calendar. validate with Hijri date. My date is already in Hijri format. I have to validate that date is valid or not with Islamic calendar.

  • In the Islamic calendar 30/02/1403 is a valid date of 2nd month.










share|improve this question




















  • 2




    Java 8 does support Islamic calendar. HijrahChronology
    – Tridev Chaudhary
    Nov 13 '18 at 9:03










  • Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
    – Ole V.V.
    Nov 13 '18 at 10:26










  • But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
    – Rameez Kandhar
    Nov 13 '18 at 10:58














3












3








3







I want to validate Hijri date. In my application Hijri date will come as in the format of dd/MM/YYYY. I want to validate whether date is valid or not in Joda Time API.



For example, I have the date:



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


Can you please help me to validate Hijri date.



My restrictions are:




  • I want to validate in Java 6. I can't use in Java 8.

  • I have to validate with Islamic calendar. validate with Hijri date. My date is already in Hijri format. I have to validate that date is valid or not with Islamic calendar.

  • In the Islamic calendar 30/02/1403 is a valid date of 2nd month.










share|improve this question















I want to validate Hijri date. In my application Hijri date will come as in the format of dd/MM/YYYY. I want to validate whether date is valid or not in Joda Time API.



For example, I have the date:



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


Can you please help me to validate Hijri date.



My restrictions are:




  • I want to validate in Java 6. I can't use in Java 8.

  • I have to validate with Islamic calendar. validate with Hijri date. My date is already in Hijri format. I have to validate that date is valid or not with Islamic calendar.

  • In the Islamic calendar 30/02/1403 is a valid date of 2nd month.







java validation jodatime java-6 hijri






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:12









Ole V.V.

27.1k62952




27.1k62952










asked Nov 13 '18 at 8:53









Rameez KandharRameez Kandhar

163




163








  • 2




    Java 8 does support Islamic calendar. HijrahChronology
    – Tridev Chaudhary
    Nov 13 '18 at 9:03










  • Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
    – Ole V.V.
    Nov 13 '18 at 10:26










  • But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
    – Rameez Kandhar
    Nov 13 '18 at 10:58














  • 2




    Java 8 does support Islamic calendar. HijrahChronology
    – Tridev Chaudhary
    Nov 13 '18 at 9:03










  • Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
    – Ole V.V.
    Nov 13 '18 at 10:26










  • But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
    – Rameez Kandhar
    Nov 13 '18 at 10:58








2




2




Java 8 does support Islamic calendar. HijrahChronology
– Tridev Chaudhary
Nov 13 '18 at 9:03




Java 8 does support Islamic calendar. HijrahChronology
– Tridev Chaudhary
Nov 13 '18 at 9:03












Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
– Ole V.V.
Nov 13 '18 at 10:26




Should 30/02/1403 be considered valid or invalid? Asking because java.time accepts it whereas ThreeTen Backport, the backport of java.time to Java 6 and 7, rejects and thinks there are only 29 days in the 2nd month of 1403.
– Ole V.V.
Nov 13 '18 at 10:26












But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
– Rameez Kandhar
Nov 13 '18 at 10:58




But If you will check the Islamic calendar it is a valid date of 2nd month. We are using Java 6. We can't change the Java version.
– Rameez Kandhar
Nov 13 '18 at 10:58












3 Answers
3






active

oldest

votes


















5














java.time



java.time, the 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 from this snippet is:




A valid date: Hijrah-umalqura AH 1403-02-30




Format pattern letters are case sensitive. You need lowercase yyyy for calendar year (or uuuu for proleptic year).



Java 6? Three options.




I want to validate in Java 6. I can't use in Java 8.




That’s old now (Java 11 is out). My suggestions are:




  1. Use ThreeTen Backport, the backport of java.time to Java 6 and 7. It turns out that it doesn’t work perfectly for your case, but you can.

  2. Use Joda-Time as you asked about from the outset.

  3. Use Time4J.


The first two options will reject your date of 30/02/1403, though, believing there are only 29 days in the 2nd 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:




Not a valid date: 30/02/1403




According to my understanding the following simpler way to construct the date ought to work, but doesn’t:



        HijrahDate date = HijrahDate.from(parsed);


I get org.threeten.bp.DateTimeException: Field not found: EpochDay. The way I construct the date instead is lenient, that is, it accepts that the day of month is out of range and produces a date of Hijrah-umalqura AH 1403-03-01. So to complete the validation I format the date back and require that I get the original string again.



If instead of 30/02/1403 I try with 29/02/1403, I get




A valid date: Hijrah-umalqura AH 1403-02-29




Joda-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 too thinks there are 29 days in this month and states that very clearly:




Not a valid date: Cannot parse "30/02/1403": Value 30 for dayOfMonth
must be in the range [1,29]




With a string of 29/02/1403 I got:




A valid date: 1403-02-29




Joda-Time’s Islamic chronology comes with four different leap year patterns. I tried all four. They all yield 29 days in the second month of 1403. I am sorry, this as close as I can get.



From the Joda-Time homepage:




Note that Joda-Time is considered to be a largely “finished” project.
No major enhancements are planned. If using Java SE 8, please migrate
to java.time (JSR-310).




Time4J



I refer you to the knowledgeable answer by Meno Hochschild, the author of Time4J. It would seem to me that Time4J version 3.50 solves your issue according to your requirements and on Java 6.



But is that date valid or not?




But If you will check the Islamic calendar it is a valid date of 2nd month.




You’re much more an expert than I am. I seem to understand that variants of the Hijri calendar exist. According to your variant 30/02/1403 is a perfectly valid date. The version given by alhabib Islamic web services agrees with yours: Safar 30, 1403 is the same day as December 16, 1982 in the Gregorian calendar. On the other hand according to searchtruth.com, there are only 29 days in the month of Safar 1403 (Safar 29, 1403 corresponds to December 15, 1982 Gregorian and is followed by Rabi Al-Awwal 1, 1403 corresponding to December 16).



Links





  • Oracle tutorial: Date Time explaining how to use java.time.


  • Java Specification Request (JSR) 310, where java.time was first described.


  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).


  • ThreeTenABP, Android edition of ThreeTen Backport

  • Joda-Time homepage

  • Time4J - Advanced Date, Time, Zone and Interval Library for Java


  • Answer by Meno Hochschild (just for the sake of completeness)

  • Islamic (Hijri) Calendar Year 1982 CE Based on Global Crescent Moon Sighting Probability on alhabib islamic web services

  • Hijri Islamic Calendar November 1982 - 1403 التقويم الهجري والميلادي on searchtruth.com






share|improve this answer























  • I want to validate in Java 6. I can't use in Java 8.
    – Rameez Kandhar
    Nov 13 '18 at 9:35










  • @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
    – Basil Bourque
    Nov 13 '18 at 16:37












  • @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
    – Meno Hochschild
    Nov 14 '18 at 5:37






  • 1




    The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
    – Meno Hochschild
    Nov 14 '18 at 5:39





















2














As supplement to the answer of Ole, I enumerate here some variants of Hijri calendar and the validation results using my lib Time4J which offers more support for variants and also offers an extra static validation method:



String variant;
for (HijriAlgorithm ha : HijriAlgorithm.values()) {
variant = ha.getVariant();
System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
}
variant = HijriCalendar.VARIANT_DIYANET;
System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
variant = HijriCalendar.VARIANT_UMALQURA;
System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));


The output is:




islamic-eastc=>false



islamic-easta=>false



islamic-civil=>false



islamic-tbla=>false



islamic-fatimidc=>false



islamic-fatimida=>false



islamic-habashalhasibc=>false



islamic-habashalhasiba=>false



islamic-diyanet=>true



islamic-umalqura=>true




So we can see that all algorithmic variants of Hijri calendar don't support your expectation but the diyanet and the umalqura variant. Probably you want the umalqura variant (the official calendar of Saudi-Arabia, but please clarify if you have more details about your expectations).



About your wish to solve your problem with Joda-Time, sorry, there is no chance to validate 1403-02-30 as valid because Joda-Time only supports four algorithmic variants (as subset of what Time4J supports). The same can be said for the Threeten-Backport which supports only "islamic-tbla" (or "islamic-civil" - here the API is not well documented). The java.time-package only supports one variant, too, but that variant (umalqura) matches your expectation. However, java.time does not run on Java 6.



If you are willing to consider an alternative to Joda-Time which matches your expectations about validation then I see mainly two ways:




  • my lib Time4J (backport version v3.50 which runs on Java 6+7)

  • msarhan






share|improve this answer





























    -1














    This is what I use:



    //import the regex class


    #import java.util.regex.*;



    Add the below inside your main class.

    String pattern = "(?:[0-2]d)|(?:30)/[0-1]d/14d{2}";

    if(Pattern.matches(pattern, yourdate)) {
    System.out.println("hijra date");
    }





    share|improve this answer























    • I want to validate in Java
      – Rameez Kandhar
      Nov 13 '18 at 9:13






    • 1




      While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
      – Ole V.V.
      Nov 13 '18 at 9:29










    • I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
      – Rameez Kandhar
      Nov 13 '18 at 9:36












    • I've edited the code, retry it.
      – Jolaosho batmat
      Nov 13 '18 at 9:51










    • If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
      – Rameez Kandhar
      Nov 13 '18 at 9:58













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277123%2fvalidation-of-hijri-date-islamic-calendar-on-java-6%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    java.time



    java.time, the 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 from this snippet is:




    A valid date: Hijrah-umalqura AH 1403-02-30




    Format pattern letters are case sensitive. You need lowercase yyyy for calendar year (or uuuu for proleptic year).



    Java 6? Three options.




    I want to validate in Java 6. I can't use in Java 8.




    That’s old now (Java 11 is out). My suggestions are:




    1. Use ThreeTen Backport, the backport of java.time to Java 6 and 7. It turns out that it doesn’t work perfectly for your case, but you can.

    2. Use Joda-Time as you asked about from the outset.

    3. Use Time4J.


    The first two options will reject your date of 30/02/1403, though, believing there are only 29 days in the 2nd 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:




    Not a valid date: 30/02/1403




    According to my understanding the following simpler way to construct the date ought to work, but doesn’t:



            HijrahDate date = HijrahDate.from(parsed);


    I get org.threeten.bp.DateTimeException: Field not found: EpochDay. The way I construct the date instead is lenient, that is, it accepts that the day of month is out of range and produces a date of Hijrah-umalqura AH 1403-03-01. So to complete the validation I format the date back and require that I get the original string again.



    If instead of 30/02/1403 I try with 29/02/1403, I get




    A valid date: Hijrah-umalqura AH 1403-02-29




    Joda-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 too thinks there are 29 days in this month and states that very clearly:




    Not a valid date: Cannot parse "30/02/1403": Value 30 for dayOfMonth
    must be in the range [1,29]




    With a string of 29/02/1403 I got:




    A valid date: 1403-02-29




    Joda-Time’s Islamic chronology comes with four different leap year patterns. I tried all four. They all yield 29 days in the second month of 1403. I am sorry, this as close as I can get.



    From the Joda-Time homepage:




    Note that Joda-Time is considered to be a largely “finished” project.
    No major enhancements are planned. If using Java SE 8, please migrate
    to java.time (JSR-310).




    Time4J



    I refer you to the knowledgeable answer by Meno Hochschild, the author of Time4J. It would seem to me that Time4J version 3.50 solves your issue according to your requirements and on Java 6.



    But is that date valid or not?




    But If you will check the Islamic calendar it is a valid date of 2nd month.




    You’re much more an expert than I am. I seem to understand that variants of the Hijri calendar exist. According to your variant 30/02/1403 is a perfectly valid date. The version given by alhabib Islamic web services agrees with yours: Safar 30, 1403 is the same day as December 16, 1982 in the Gregorian calendar. On the other hand according to searchtruth.com, there are only 29 days in the month of Safar 1403 (Safar 29, 1403 corresponds to December 15, 1982 Gregorian and is followed by Rabi Al-Awwal 1, 1403 corresponding to December 16).



    Links





    • Oracle tutorial: Date Time explaining how to use java.time.


    • Java Specification Request (JSR) 310, where java.time was first described.


    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).


    • ThreeTenABP, Android edition of ThreeTen Backport

    • Joda-Time homepage

    • Time4J - Advanced Date, Time, Zone and Interval Library for Java


    • Answer by Meno Hochschild (just for the sake of completeness)

    • Islamic (Hijri) Calendar Year 1982 CE Based on Global Crescent Moon Sighting Probability on alhabib islamic web services

    • Hijri Islamic Calendar November 1982 - 1403 التقويم الهجري والميلادي on searchtruth.com






    share|improve this answer























    • I want to validate in Java 6. I can't use in Java 8.
      – Rameez Kandhar
      Nov 13 '18 at 9:35










    • @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
      – Basil Bourque
      Nov 13 '18 at 16:37












    • @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
      – Meno Hochschild
      Nov 14 '18 at 5:37






    • 1




      The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
      – Meno Hochschild
      Nov 14 '18 at 5:39


















    5














    java.time



    java.time, the 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 from this snippet is:




    A valid date: Hijrah-umalqura AH 1403-02-30




    Format pattern letters are case sensitive. You need lowercase yyyy for calendar year (or uuuu for proleptic year).



    Java 6? Three options.




    I want to validate in Java 6. I can't use in Java 8.




    That’s old now (Java 11 is out). My suggestions are:




    1. Use ThreeTen Backport, the backport of java.time to Java 6 and 7. It turns out that it doesn’t work perfectly for your case, but you can.

    2. Use Joda-Time as you asked about from the outset.

    3. Use Time4J.


    The first two options will reject your date of 30/02/1403, though, believing there are only 29 days in the 2nd 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:




    Not a valid date: 30/02/1403




    According to my understanding the following simpler way to construct the date ought to work, but doesn’t:



            HijrahDate date = HijrahDate.from(parsed);


    I get org.threeten.bp.DateTimeException: Field not found: EpochDay. The way I construct the date instead is lenient, that is, it accepts that the day of month is out of range and produces a date of Hijrah-umalqura AH 1403-03-01. So to complete the validation I format the date back and require that I get the original string again.



    If instead of 30/02/1403 I try with 29/02/1403, I get




    A valid date: Hijrah-umalqura AH 1403-02-29




    Joda-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 too thinks there are 29 days in this month and states that very clearly:




    Not a valid date: Cannot parse "30/02/1403": Value 30 for dayOfMonth
    must be in the range [1,29]




    With a string of 29/02/1403 I got:




    A valid date: 1403-02-29




    Joda-Time’s Islamic chronology comes with four different leap year patterns. I tried all four. They all yield 29 days in the second month of 1403. I am sorry, this as close as I can get.



    From the Joda-Time homepage:




    Note that Joda-Time is considered to be a largely “finished” project.
    No major enhancements are planned. If using Java SE 8, please migrate
    to java.time (JSR-310).




    Time4J



    I refer you to the knowledgeable answer by Meno Hochschild, the author of Time4J. It would seem to me that Time4J version 3.50 solves your issue according to your requirements and on Java 6.



    But is that date valid or not?




    But If you will check the Islamic calendar it is a valid date of 2nd month.




    You’re much more an expert than I am. I seem to understand that variants of the Hijri calendar exist. According to your variant 30/02/1403 is a perfectly valid date. The version given by alhabib Islamic web services agrees with yours: Safar 30, 1403 is the same day as December 16, 1982 in the Gregorian calendar. On the other hand according to searchtruth.com, there are only 29 days in the month of Safar 1403 (Safar 29, 1403 corresponds to December 15, 1982 Gregorian and is followed by Rabi Al-Awwal 1, 1403 corresponding to December 16).



    Links





    • Oracle tutorial: Date Time explaining how to use java.time.


    • Java Specification Request (JSR) 310, where java.time was first described.


    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).


    • ThreeTenABP, Android edition of ThreeTen Backport

    • Joda-Time homepage

    • Time4J - Advanced Date, Time, Zone and Interval Library for Java


    • Answer by Meno Hochschild (just for the sake of completeness)

    • Islamic (Hijri) Calendar Year 1982 CE Based on Global Crescent Moon Sighting Probability on alhabib islamic web services

    • Hijri Islamic Calendar November 1982 - 1403 التقويم الهجري والميلادي on searchtruth.com






    share|improve this answer























    • I want to validate in Java 6. I can't use in Java 8.
      – Rameez Kandhar
      Nov 13 '18 at 9:35










    • @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
      – Basil Bourque
      Nov 13 '18 at 16:37












    • @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
      – Meno Hochschild
      Nov 14 '18 at 5:37






    • 1




      The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
      – Meno Hochschild
      Nov 14 '18 at 5:39
















    5












    5








    5






    java.time



    java.time, the 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 from this snippet is:




    A valid date: Hijrah-umalqura AH 1403-02-30




    Format pattern letters are case sensitive. You need lowercase yyyy for calendar year (or uuuu for proleptic year).



    Java 6? Three options.




    I want to validate in Java 6. I can't use in Java 8.




    That’s old now (Java 11 is out). My suggestions are:




    1. Use ThreeTen Backport, the backport of java.time to Java 6 and 7. It turns out that it doesn’t work perfectly for your case, but you can.

    2. Use Joda-Time as you asked about from the outset.

    3. Use Time4J.


    The first two options will reject your date of 30/02/1403, though, believing there are only 29 days in the 2nd 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:




    Not a valid date: 30/02/1403




    According to my understanding the following simpler way to construct the date ought to work, but doesn’t:



            HijrahDate date = HijrahDate.from(parsed);


    I get org.threeten.bp.DateTimeException: Field not found: EpochDay. The way I construct the date instead is lenient, that is, it accepts that the day of month is out of range and produces a date of Hijrah-umalqura AH 1403-03-01. So to complete the validation I format the date back and require that I get the original string again.



    If instead of 30/02/1403 I try with 29/02/1403, I get




    A valid date: Hijrah-umalqura AH 1403-02-29




    Joda-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 too thinks there are 29 days in this month and states that very clearly:




    Not a valid date: Cannot parse "30/02/1403": Value 30 for dayOfMonth
    must be in the range [1,29]




    With a string of 29/02/1403 I got:




    A valid date: 1403-02-29




    Joda-Time’s Islamic chronology comes with four different leap year patterns. I tried all four. They all yield 29 days in the second month of 1403. I am sorry, this as close as I can get.



    From the Joda-Time homepage:




    Note that Joda-Time is considered to be a largely “finished” project.
    No major enhancements are planned. If using Java SE 8, please migrate
    to java.time (JSR-310).




    Time4J



    I refer you to the knowledgeable answer by Meno Hochschild, the author of Time4J. It would seem to me that Time4J version 3.50 solves your issue according to your requirements and on Java 6.



    But is that date valid or not?




    But If you will check the Islamic calendar it is a valid date of 2nd month.




    You’re much more an expert than I am. I seem to understand that variants of the Hijri calendar exist. According to your variant 30/02/1403 is a perfectly valid date. The version given by alhabib Islamic web services agrees with yours: Safar 30, 1403 is the same day as December 16, 1982 in the Gregorian calendar. On the other hand according to searchtruth.com, there are only 29 days in the month of Safar 1403 (Safar 29, 1403 corresponds to December 15, 1982 Gregorian and is followed by Rabi Al-Awwal 1, 1403 corresponding to December 16).



    Links





    • Oracle tutorial: Date Time explaining how to use java.time.


    • Java Specification Request (JSR) 310, where java.time was first described.


    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).


    • ThreeTenABP, Android edition of ThreeTen Backport

    • Joda-Time homepage

    • Time4J - Advanced Date, Time, Zone and Interval Library for Java


    • Answer by Meno Hochschild (just for the sake of completeness)

    • Islamic (Hijri) Calendar Year 1982 CE Based on Global Crescent Moon Sighting Probability on alhabib islamic web services

    • Hijri Islamic Calendar November 1982 - 1403 التقويم الهجري والميلادي on searchtruth.com






    share|improve this answer














    java.time



    java.time, the 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 from this snippet is:




    A valid date: Hijrah-umalqura AH 1403-02-30




    Format pattern letters are case sensitive. You need lowercase yyyy for calendar year (or uuuu for proleptic year).



    Java 6? Three options.




    I want to validate in Java 6. I can't use in Java 8.




    That’s old now (Java 11 is out). My suggestions are:




    1. Use ThreeTen Backport, the backport of java.time to Java 6 and 7. It turns out that it doesn’t work perfectly for your case, but you can.

    2. Use Joda-Time as you asked about from the outset.

    3. Use Time4J.


    The first two options will reject your date of 30/02/1403, though, believing there are only 29 days in the 2nd 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:




    Not a valid date: 30/02/1403




    According to my understanding the following simpler way to construct the date ought to work, but doesn’t:



            HijrahDate date = HijrahDate.from(parsed);


    I get org.threeten.bp.DateTimeException: Field not found: EpochDay. The way I construct the date instead is lenient, that is, it accepts that the day of month is out of range and produces a date of Hijrah-umalqura AH 1403-03-01. So to complete the validation I format the date back and require that I get the original string again.



    If instead of 30/02/1403 I try with 29/02/1403, I get




    A valid date: Hijrah-umalqura AH 1403-02-29




    Joda-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 too thinks there are 29 days in this month and states that very clearly:




    Not a valid date: Cannot parse "30/02/1403": Value 30 for dayOfMonth
    must be in the range [1,29]




    With a string of 29/02/1403 I got:




    A valid date: 1403-02-29




    Joda-Time’s Islamic chronology comes with four different leap year patterns. I tried all four. They all yield 29 days in the second month of 1403. I am sorry, this as close as I can get.



    From the Joda-Time homepage:




    Note that Joda-Time is considered to be a largely “finished” project.
    No major enhancements are planned. If using Java SE 8, please migrate
    to java.time (JSR-310).




    Time4J



    I refer you to the knowledgeable answer by Meno Hochschild, the author of Time4J. It would seem to me that Time4J version 3.50 solves your issue according to your requirements and on Java 6.



    But is that date valid or not?




    But If you will check the Islamic calendar it is a valid date of 2nd month.




    You’re much more an expert than I am. I seem to understand that variants of the Hijri calendar exist. According to your variant 30/02/1403 is a perfectly valid date. The version given by alhabib Islamic web services agrees with yours: Safar 30, 1403 is the same day as December 16, 1982 in the Gregorian calendar. On the other hand according to searchtruth.com, there are only 29 days in the month of Safar 1403 (Safar 29, 1403 corresponds to December 15, 1982 Gregorian and is followed by Rabi Al-Awwal 1, 1403 corresponding to December 16).



    Links





    • Oracle tutorial: Date Time explaining how to use java.time.


    • Java Specification Request (JSR) 310, where java.time was first described.


    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).


    • ThreeTenABP, Android edition of ThreeTen Backport

    • Joda-Time homepage

    • Time4J - Advanced Date, Time, Zone and Interval Library for Java


    • Answer by Meno Hochschild (just for the sake of completeness)

    • Islamic (Hijri) Calendar Year 1982 CE Based on Global Crescent Moon Sighting Probability on alhabib islamic web services

    • Hijri Islamic Calendar November 1982 - 1403 التقويم الهجري والميلادي on searchtruth.com







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 8:36









    Meno Hochschild

    29.6k56095




    29.6k56095










    answered Nov 13 '18 at 9:21









    Ole V.V.Ole V.V.

    27.1k62952




    27.1k62952












    • I want to validate in Java 6. I can't use in Java 8.
      – Rameez Kandhar
      Nov 13 '18 at 9:35










    • @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
      – Basil Bourque
      Nov 13 '18 at 16:37












    • @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
      – Meno Hochschild
      Nov 14 '18 at 5:37






    • 1




      The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
      – Meno Hochschild
      Nov 14 '18 at 5:39




















    • I want to validate in Java 6. I can't use in Java 8.
      – Rameez Kandhar
      Nov 13 '18 at 9:35










    • @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
      – Basil Bourque
      Nov 13 '18 at 16:37












    • @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
      – Meno Hochschild
      Nov 14 '18 at 5:37






    • 1




      The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
      – Meno Hochschild
      Nov 14 '18 at 5:39


















    I want to validate in Java 6. I can't use in Java 8.
    – Rameez Kandhar
    Nov 13 '18 at 9:35




    I want to validate in Java 6. I can't use in Java 8.
    – Rameez Kandhar
    Nov 13 '18 at 9:35












    @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
    – Basil Bourque
    Nov 13 '18 at 16:37






    @RameezKandhar Use the ThreeTen-Backport library to get most of the java.time functionality in Java 6 & 7 with virtually the same API as in Java 8.
    – Basil Bourque
    Nov 13 '18 at 16:37














    @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
    – Meno Hochschild
    Nov 14 '18 at 5:37




    @BasilBourque Why do you recommend a library which does not match the explicitly stated validation expectation of OP?
    – Meno Hochschild
    Nov 14 '18 at 5:37




    1




    1




    The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
    – Meno Hochschild
    Nov 14 '18 at 5:39






    The searthtruth.com-website also says: "Probability of 1 day difference." So its calendar just seems to be an algorithmic approximation. Another thing: I have added my answer to demonstrate how to validate with Time4J.
    – Meno Hochschild
    Nov 14 '18 at 5:39















    2














    As supplement to the answer of Ole, I enumerate here some variants of Hijri calendar and the validation results using my lib Time4J which offers more support for variants and also offers an extra static validation method:



    String variant;
    for (HijriAlgorithm ha : HijriAlgorithm.values()) {
    variant = ha.getVariant();
    System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
    }
    variant = HijriCalendar.VARIANT_DIYANET;
    System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
    variant = HijriCalendar.VARIANT_UMALQURA;
    System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));


    The output is:




    islamic-eastc=>false



    islamic-easta=>false



    islamic-civil=>false



    islamic-tbla=>false



    islamic-fatimidc=>false



    islamic-fatimida=>false



    islamic-habashalhasibc=>false



    islamic-habashalhasiba=>false



    islamic-diyanet=>true



    islamic-umalqura=>true




    So we can see that all algorithmic variants of Hijri calendar don't support your expectation but the diyanet and the umalqura variant. Probably you want the umalqura variant (the official calendar of Saudi-Arabia, but please clarify if you have more details about your expectations).



    About your wish to solve your problem with Joda-Time, sorry, there is no chance to validate 1403-02-30 as valid because Joda-Time only supports four algorithmic variants (as subset of what Time4J supports). The same can be said for the Threeten-Backport which supports only "islamic-tbla" (or "islamic-civil" - here the API is not well documented). The java.time-package only supports one variant, too, but that variant (umalqura) matches your expectation. However, java.time does not run on Java 6.



    If you are willing to consider an alternative to Joda-Time which matches your expectations about validation then I see mainly two ways:




    • my lib Time4J (backport version v3.50 which runs on Java 6+7)

    • msarhan






    share|improve this answer


























      2














      As supplement to the answer of Ole, I enumerate here some variants of Hijri calendar and the validation results using my lib Time4J which offers more support for variants and also offers an extra static validation method:



      String variant;
      for (HijriAlgorithm ha : HijriAlgorithm.values()) {
      variant = ha.getVariant();
      System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
      }
      variant = HijriCalendar.VARIANT_DIYANET;
      System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
      variant = HijriCalendar.VARIANT_UMALQURA;
      System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));


      The output is:




      islamic-eastc=>false



      islamic-easta=>false



      islamic-civil=>false



      islamic-tbla=>false



      islamic-fatimidc=>false



      islamic-fatimida=>false



      islamic-habashalhasibc=>false



      islamic-habashalhasiba=>false



      islamic-diyanet=>true



      islamic-umalqura=>true




      So we can see that all algorithmic variants of Hijri calendar don't support your expectation but the diyanet and the umalqura variant. Probably you want the umalqura variant (the official calendar of Saudi-Arabia, but please clarify if you have more details about your expectations).



      About your wish to solve your problem with Joda-Time, sorry, there is no chance to validate 1403-02-30 as valid because Joda-Time only supports four algorithmic variants (as subset of what Time4J supports). The same can be said for the Threeten-Backport which supports only "islamic-tbla" (or "islamic-civil" - here the API is not well documented). The java.time-package only supports one variant, too, but that variant (umalqura) matches your expectation. However, java.time does not run on Java 6.



      If you are willing to consider an alternative to Joda-Time which matches your expectations about validation then I see mainly two ways:




      • my lib Time4J (backport version v3.50 which runs on Java 6+7)

      • msarhan






      share|improve this answer
























        2












        2








        2






        As supplement to the answer of Ole, I enumerate here some variants of Hijri calendar and the validation results using my lib Time4J which offers more support for variants and also offers an extra static validation method:



        String variant;
        for (HijriAlgorithm ha : HijriAlgorithm.values()) {
        variant = ha.getVariant();
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
        }
        variant = HijriCalendar.VARIANT_DIYANET;
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
        variant = HijriCalendar.VARIANT_UMALQURA;
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));


        The output is:




        islamic-eastc=>false



        islamic-easta=>false



        islamic-civil=>false



        islamic-tbla=>false



        islamic-fatimidc=>false



        islamic-fatimida=>false



        islamic-habashalhasibc=>false



        islamic-habashalhasiba=>false



        islamic-diyanet=>true



        islamic-umalqura=>true




        So we can see that all algorithmic variants of Hijri calendar don't support your expectation but the diyanet and the umalqura variant. Probably you want the umalqura variant (the official calendar of Saudi-Arabia, but please clarify if you have more details about your expectations).



        About your wish to solve your problem with Joda-Time, sorry, there is no chance to validate 1403-02-30 as valid because Joda-Time only supports four algorithmic variants (as subset of what Time4J supports). The same can be said for the Threeten-Backport which supports only "islamic-tbla" (or "islamic-civil" - here the API is not well documented). The java.time-package only supports one variant, too, but that variant (umalqura) matches your expectation. However, java.time does not run on Java 6.



        If you are willing to consider an alternative to Joda-Time which matches your expectations about validation then I see mainly two ways:




        • my lib Time4J (backport version v3.50 which runs on Java 6+7)

        • msarhan






        share|improve this answer












        As supplement to the answer of Ole, I enumerate here some variants of Hijri calendar and the validation results using my lib Time4J which offers more support for variants and also offers an extra static validation method:



        String variant;
        for (HijriAlgorithm ha : HijriAlgorithm.values()) {
        variant = ha.getVariant();
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
        }
        variant = HijriCalendar.VARIANT_DIYANET;
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));
        variant = HijriCalendar.VARIANT_UMALQURA;
        System.out.println(variant + "=>" + HijriCalendar.isValid(variant, 1403, 2, 30));


        The output is:




        islamic-eastc=>false



        islamic-easta=>false



        islamic-civil=>false



        islamic-tbla=>false



        islamic-fatimidc=>false



        islamic-fatimida=>false



        islamic-habashalhasibc=>false



        islamic-habashalhasiba=>false



        islamic-diyanet=>true



        islamic-umalqura=>true




        So we can see that all algorithmic variants of Hijri calendar don't support your expectation but the diyanet and the umalqura variant. Probably you want the umalqura variant (the official calendar of Saudi-Arabia, but please clarify if you have more details about your expectations).



        About your wish to solve your problem with Joda-Time, sorry, there is no chance to validate 1403-02-30 as valid because Joda-Time only supports four algorithmic variants (as subset of what Time4J supports). The same can be said for the Threeten-Backport which supports only "islamic-tbla" (or "islamic-civil" - here the API is not well documented). The java.time-package only supports one variant, too, but that variant (umalqura) matches your expectation. However, java.time does not run on Java 6.



        If you are willing to consider an alternative to Joda-Time which matches your expectations about validation then I see mainly two ways:




        • my lib Time4J (backport version v3.50 which runs on Java 6+7)

        • msarhan







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 5:32









        Meno HochschildMeno Hochschild

        29.6k56095




        29.6k56095























            -1














            This is what I use:



            //import the regex class


            #import java.util.regex.*;



            Add the below inside your main class.

            String pattern = "(?:[0-2]d)|(?:30)/[0-1]d/14d{2}";

            if(Pattern.matches(pattern, yourdate)) {
            System.out.println("hijra date");
            }





            share|improve this answer























            • I want to validate in Java
              – Rameez Kandhar
              Nov 13 '18 at 9:13






            • 1




              While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
              – Ole V.V.
              Nov 13 '18 at 9:29










            • I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
              – Rameez Kandhar
              Nov 13 '18 at 9:36












            • I've edited the code, retry it.
              – Jolaosho batmat
              Nov 13 '18 at 9:51










            • If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
              – Rameez Kandhar
              Nov 13 '18 at 9:58


















            -1














            This is what I use:



            //import the regex class


            #import java.util.regex.*;



            Add the below inside your main class.

            String pattern = "(?:[0-2]d)|(?:30)/[0-1]d/14d{2}";

            if(Pattern.matches(pattern, yourdate)) {
            System.out.println("hijra date");
            }





            share|improve this answer























            • I want to validate in Java
              – Rameez Kandhar
              Nov 13 '18 at 9:13






            • 1




              While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
              – Ole V.V.
              Nov 13 '18 at 9:29










            • I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
              – Rameez Kandhar
              Nov 13 '18 at 9:36












            • I've edited the code, retry it.
              – Jolaosho batmat
              Nov 13 '18 at 9:51










            • If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
              – Rameez Kandhar
              Nov 13 '18 at 9:58
















            -1












            -1








            -1






            This is what I use:



            //import the regex class


            #import java.util.regex.*;



            Add the below inside your main class.

            String pattern = "(?:[0-2]d)|(?:30)/[0-1]d/14d{2}";

            if(Pattern.matches(pattern, yourdate)) {
            System.out.println("hijra date");
            }





            share|improve this answer














            This is what I use:



            //import the regex class


            #import java.util.regex.*;



            Add the below inside your main class.

            String pattern = "(?:[0-2]d)|(?:30)/[0-1]d/14d{2}";

            if(Pattern.matches(pattern, yourdate)) {
            System.out.println("hijra date");
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 12:51

























            answered Nov 13 '18 at 9:04









            Jolaosho batmatJolaosho batmat

            94




            94












            • I want to validate in Java
              – Rameez Kandhar
              Nov 13 '18 at 9:13






            • 1




              While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
              – Ole V.V.
              Nov 13 '18 at 9:29










            • I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
              – Rameez Kandhar
              Nov 13 '18 at 9:36












            • I've edited the code, retry it.
              – Jolaosho batmat
              Nov 13 '18 at 9:51










            • If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
              – Rameez Kandhar
              Nov 13 '18 at 9:58




















            • I want to validate in Java
              – Rameez Kandhar
              Nov 13 '18 at 9:13






            • 1




              While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
              – Ole V.V.
              Nov 13 '18 at 9:29










            • I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
              – Rameez Kandhar
              Nov 13 '18 at 9:36












            • I've edited the code, retry it.
              – Jolaosho batmat
              Nov 13 '18 at 9:51










            • If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
              – Rameez Kandhar
              Nov 13 '18 at 9:58


















            I want to validate in Java
            – Rameez Kandhar
            Nov 13 '18 at 9:13




            I want to validate in Java
            – Rameez Kandhar
            Nov 13 '18 at 9:13




            1




            1




            While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
            – Ole V.V.
            Nov 13 '18 at 9:29




            While I would prefer using java.time, the code in this answer is Java. @RameezKandhar
            – Ole V.V.
            Nov 13 '18 at 9:29












            I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
            – Rameez Kandhar
            Nov 13 '18 at 9:36






            I tried with below code but it always return false. String date = "30/02/1403"; String pattern = "[0123]\d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); }
            – Rameez Kandhar
            Nov 13 '18 at 9:36














            I've edited the code, retry it.
            – Jolaosho batmat
            Nov 13 '18 at 9:51




            I've edited the code, retry it.
            – Jolaosho batmat
            Nov 13 '18 at 9:51












            If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
            – Rameez Kandhar
            Nov 13 '18 at 9:58






            If, I am using your code then I am getting compile time error for invalid escape sequence and I have added two backward slates and now the code like this : String date = "30/02/1403"; String pattern = "[0123]d\/[01]d\/14d{2}"; System.out.println(Pattern.matches(pattern, date)); if(Pattern.matches(pattern, date)) { System.out.println("hijra date"); } still I am getting false.
            – Rameez Kandhar
            Nov 13 '18 at 9:58




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277123%2fvalidation-of-hijri-date-islamic-calendar-on-java-6%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python