Assume that I have two LocalDate objects, fromDate and toDate. I need to know which week days are there inside the interval - Monday, Tuesday, etc. And, I'd like to know also which daysOfMonth - 1st, 2nd, or, for example 20th only - from 20 to 23rd. Is there any useful tool/method in the Joda time library which cah help me? I'm not asking about particular algorithm, I could write it by myself, but I'm not sure about Joda functionality - maybe it already has things I could reuse.
-
FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 28 '18 at 21:04
3 Answers
This is about as close as you'll get I think :
or maybe something like this (assumes only ever two different months) :
public Boolean dayOfMonthExistInPeriod(final int dayOfMonth, final LocalDate from, final LocalDate until){
final DateMidnight tested = new DateMidnight(from.getYear(), from.getMonthOfYear(), dayOfMonth);
final DateMidnight tested2 = new DateMidnight(until.getYear(), until.getMonthOfYear(), dayOfMonth);
final Interval interval = new Interval(from.toDateMidnight(), until.toDateMidnight());
return interval.contains(tested) && interval.contains(tested2);
}

- 46,453
- 60
- 198
- 311
-
But it will allow me just only count how many days was between two dates, which is not so important for me. Of course, if this amount was > 7 then I can be sure that this interval includes all the week days, from Mon to Sun. But, what if interval less than 7 days? I don't know whether it includes Monday unless I will iterate over every day which looks like a nightmare... It gets worse if I start thinking about how to decide whether this interval include 28th day of month or no.. – javagirl Apr 11 '12 at 13:16
-
its hardly a nightmare - you wouldn't need to iterate over every one to find out if a dayofweek was included; there are the dayOfMonth() and dayOfWeek() properties too. – NimChimpsky Apr 11 '12 at 13:20
-
Class Days does not consist of these properties, it's LocalDate properties.. so you suggest to iterate over every day in the interval to check dayOfMonth() and dayOfWeek() ? – javagirl Apr 11 '12 at 13:33
-
No I don't suggest that. I am not sure of you exact requirements, but its pretty simple calculation to find out whether a given date is present, or a given day of week - without iterating over every day in the period. – NimChimpsky Apr 11 '12 at 13:40
-
I'm sure that if it is simple you wouldn't mind write short simple code snippet to calculate whether interval 11.04.2012-02.05.2012 contains 31th day of month – javagirl Apr 11 '12 at 13:50
-
-
Why does your method return `Boolean` instead of `boolean`? Do you ever expect to return null? – Steve Kuo Apr 11 '12 at 15:52
-
@SteveKuo I have got into the habit of not using the primitives for such reasons as being null safe and having built in additional functionality. I assume the jvm handles any significant optimization. – NimChimpsky Apr 11 '12 at 15:54
-
The issue isn't optimization, but semantics. Looking at the signature of your method implies that null can be returned. By using `Boolean` instead of `boolean` you are introducing unnecessary complexity. – Steve Kuo Apr 12 '12 at 23:26
-
@NimChimpsky the task is no longer actual, however will +1 just for the attention and your time – javagirl Apr 19 '12 at 18:49
-
Update: The midnight-related classes and methods in Joda-Time 2.3 are deprecated. Instead call the DateTime method `withTimeAtStartOfDay`. – Basil Bourque Apr 19 '14 at 15:59
public static boolean isDayBetween(int dtc, DateTime from, DateTime to)
{
final int dayOfWeekFrom = from.dayOfWeek().get();
final int daysBetween = Days.daysBetween(from, to).getDays();
if (dtc >= dayOfWeekFrom && dtc <= (dayOfWeekFrom + daysBetween))
return true;
return false;
}
Use method
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
final DateTime from = dtf.parseDateTime("2000-07-08");
final DateTime to = dtf.parseDateTime("2000-07-13");
boolean res = isDayBetween(from, to, DateTimeConstants.THURSDAY)

- 29,135
- 19
- 110
- 158
tl;dr
java.time.LocalDate.of( 2018 , Month.JANUARY , 23 )
.plusDays( 1 )
.getDayOfWeek() // Returns a `DayOfWeek` enum object such as `DayOfWeek.MONDAY`.
…and…
java.time.LocalDate.of( 2018 , Month.JANUARY , 23 )
.plusDays( 1 )
.getDayOfMonth() // Returns an integer number, 1-31.
java.time
The modern approach uses the java.time classes that supplanted both the legacy date-time classes from the earliest Java and the Joda-Time project. Both Joda-Time and java.time/JSR 310 are led by the same man, Stephen Colebourne, so the concepts are closely aligned.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate start = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
LocalDate stop = LocalDate.of( 2018 , Month.FEBRUARY , 4 ) ;
Calculating number of days elapsed is easy with the ChronoUnit
enum.
long days = ChronoUnit.DAYS.between( start , stop ) ;
That calculation is made using the Half-Open approach where the beginning is inclusive while the ending is exclusive. This approach is generally the wise way to define spans of time.
I need to know which week days are there inside the interval - Monday, Tuesday, etc.
There’s a class for that: DayOfWeek
, an enum defining seven objects, one for each day of the week. The EnumSet
class is a highly-optimized implementation of Set
for collecting DayOfWeek
objects. A Set
absorbs/ignores duplicates, so we will end up with a distinct collection.
final int countDaysInWeek = DayOfWeek.values().length ; // Seven days in a week.
Set< DayOfWeek > dows = EnumSet.noneOf( DayOfWeek.class ) ;
LocalDate ld = start ;
while ( ld.isBefore( stop ) ) {
dows.add( ld.getDayOfWeek() ) ; // Add each date’s day-of-week to our collection.
if ( dows.size() == countDaysInWeek ) { break ; } // Bail out of this loop after collecting all the days of the week.
ld = ld.plusDays( 1 ) ; // Increment to the next day, to setup the next loop. The java.time classes use immutable objects, so we are generating a fresh object here rather than modifying the original.
}
And, I'd like to know also which daysOfMonth - 1st, 2nd, or, for example 20th only - from 20 to 23rd.
You could collect the number of the day-of-month, but I doubt that is what you want.
List< Integer > dayOfMonthNumbers = new ArrayList<>() ;
LocalDate ld = start ;
while ( ld.isBefore( stop ) ) {
dayOfMonthNumbers.add( ld.getDayOfMonth() ) ;
ld = ld.plusDays( 1 ) ; // Increment to next date.
}
If you roll into another month you will end up with a list like: 29 , 30 , 1 , 2
. I suspect you will find it more useful to use a collection of LocalDate
objects rather than a mere day-of-month number.
List< LocalDate > dates = new ArrayList<>() ;
LocalDate ld = start ;
while ( ld.isBefore( stop ) ) {
dates.add( ld ) ;
ld = ld.plusDays( 1 ) ; // Increment to next date.
}
From there you can generate Strings to represent those date values using DateTimeFormatter
. Search Stack Overflow for more info, as that has been covered many times already.
ThreeTen-Extra & LocalDateRange
Tip: If you add the ThreeTen-Extra library to your project, you can represent your span of time using the LocalDateRange
class.
LocalDateRange range = LocalDateRange.of( start , stop ) ;
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?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.

- 303,325
- 100
- 852
- 1,154