tl;dr
HijrahDate
.from
(
LocalDate.of( 2020 , Month.JANUARY , 23 )
)
.plus
(
Period.ofDays( 5 )
)
.isBefore
(
someOtherDate
)
Details
The modern approach uses the java.time classes built into Java 8 and later, and Android 26 and later. For earlier versions, see the ThreeTen-Backport and ThreeTenABP projects.
The Joda-Time project is the predecessor of java.time. Both are founded and led by the same man, Stephen Colebourne.
The HijrahChronology
is part of java.time. The HijrahChronology
follows the rules of the Hijrah calendar system. The Hijrah calendar is a lunar calendar supporting Islamic calendars.
The HijrahDate
class represents a date in the Hijrah calendar system.
Search Stack Overflow to learn more, as this has been covered multiple times already.
I am not an expert on Islamic calendars or HijrahChronology
, but I believe you can convert between a Hijrah date and a Gregorian (proleptic) date by calling the from
method on HijrahDate
and LocalDate
classes.
LocalDate localDate1 = LocalDate.of( 2020 , Month.JANUARY , 23 ) ;
HijrahDate hijrahDate = HijrahDate.from( localDate1 ) ;
…and…
LocalDate localDate2 = LocalDate.from( hijrahDate ) ;
Both date classes offer plus
and minus
methods that take a Period
object.
LocalDate later = localDate2.plus( Period.ofDays( 5 ) ) ;
Compare with isEqual
, isBefore
, and isAfter
.
boolean match = localDate1.isEqual( localDate2 ) ;
Dump to console.
System.out.println( "localDate1.toString(): " + localDate1 ) ;
System.out.println( "hijrahDate.toString(): " + hijrahDate ) ;
System.out.println( "localDate2.toString(): " + localDate2 ) ;
System.out.println( "match: " + localDate1.isEqual( localDate2 ) ) ;
System.out.println( "later.toString(): " + later ) ;
See this code run live at IdeOne.com.
localDate1.toString(): 2020-01-23
hijrahDate.toString(): Hijrah-umalqura AH 1441-05-28
localDate2.toString(): 2020-01-23
match: true
later.toString(): 2020-01-28
Here is a complete example. The comparison method for a span-of-time used here is Half-Open, where the beginning is inclusive while the ending is exclusive. This approach is usually best in date-time handling. This approach lets date ranges neatly abut one another without gaps or overlaps.
LocalDate localDate = LocalDate.of( 2020 , Month.JANUARY , 23 );
HijrahDate hijrahDate = HijrahDate.from( localDate );
// Target range is five days before and after some specific date.
HijrahDate target = HijrahDate.from( LocalDate.of( 2020 , Month.MARCH , 14 ) );
Period period = Period.ofDays( 5 );
LocalDate start = LocalDate.from( target ).minus( period );
LocalDate end = LocalDate.from( target ).plus( period );
// Tip: A shorter way of asking "Is equal to or later" is "Is not before".
boolean withinTargetRange = ( ! hijrahDate.isBefore( start ) ) && hijrahDate.isBefore( end );
Dump to console.
System.out.println( "localDate = " + localDate );
System.out.println( "hijrahDate = " + hijrahDate );
System.out.println( "target = " + target );
System.out.println( "withinTargetRange = " + withinTargetRange );
localDate = 2020-01-23
hijrahDate = Hijrah-umalqura AH 1441-05-28
target = Hijrah-umalqura AH 1441-07-19
withinTargetRange = false
ThreeTen-Extra
If doing much of this work, I suggest adding the ThreeTen-Extra library, also founded and led by Stephen Colebourne. The LocalDateRange
class in that library represents a span of time as a pair of LocalDate
objects. This class includes methods such as contains
, abuts
, overlaps
, and more. I do not know how well this library works in Android. If not working well in Android, you may want to pull a copy of the class’s source code into your codebase, provided you can abide by the terms of its BSD-3 licensing.
Here is an example using LocalDateRange::contains
.
LocalDate localDate = LocalDate.of( 2020 , Month.JANUARY , 23 );
HijrahDate hijrahDate = HijrahDate.from( localDate );
HijrahDate target = HijrahDate.from( LocalDate.of( 2020 , Month.MARCH , 14 ) );
Period period = Period.ofDays( 5 );
LocalDate start = LocalDate.from( target ).minus( period );
LocalDate end = LocalDate.from( target ).plus( period );
LocalDateRange range = LocalDateRange.of( start , end );
boolean withinTargetRange = range.contains( LocalDate.from( hijrahDate ) );
localDate = 2020-01-23
hijrahDate = Hijrah-umalqura AH 1441-05-28
target = Hijrah-umalqura AH 1441-07-19
range = 2020-03-09/2020-03-19
withinTargetRange = false