41

In Joda-Time, is there a way to get the date of the first day of the week(monday).

for instance i want to find out what date was this weeks monday based on todays current date 21/01/11

Cheers in advance.

edit: i also wish to find the date for the end of the week i.e sunday's date. cheers

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jono
  • 17,341
  • 48
  • 135
  • 217

5 Answers5

74

Try LocalDate.withDayOfWeek:

LocalDate now = new LocalDate();
System.out.println(now.withDayOfWeek(DateTimeConstants.MONDAY)); //prints 2011-01-17
System.out.println(now.withDayOfWeek(DateTimeConstants.SUNDAY)); //prints 2011-01-23
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Further tip: You can call `isBefore`/`isAfter` and `minusWeeks`/`plusWeeks` to get past/future values. – Basil Bourque Dec 08 '13 at 23:57
  • 3
    Probably will not work for countries where week starts Sundays or Saturdays – Eugen Martynov Nov 11 '15 at 12:19
  • I know this is old, but I'm using Java 8 and those methods definitely do NOT exist anymore. I still can't find a solution to this problem that doesn't use Calendar. – Zephyr Aug 25 '17 at 21:42
  • @Zephyr Those methods are not core Java methods, but from the Joda-Time library (see question). See [this answer](https://stackoverflow.com/a/33591149/1288408) for a solution using the new Date framework from Java8. – Modus Tollens Aug 25 '17 at 21:53
27
LocalDate today = new LocalDate();
LocalDate weekStart = today.dayOfWeek().withMinimumValue();
LocalDate weekEnd = today.dayOfWeek().withMaximumValue();

Will give you the first and last days i.e Monday and sunday

ThomasRS
  • 8,215
  • 5
  • 33
  • 48
sudhir
  • 381
  • 4
  • 5
  • I don't think those methods exists anymore, I'm not finding them now. – Dandre Allison Jan 11 '13 at 22:16
  • @Dandre Allison please, note it is LocalDate. Methods are still there. – divonas May 28 '15 at 11:26
  • @sudhir Thanks, this is better answer then the accepted one. works with DateTime as well. – vijay Jul 30 '16 at 22:42
  • 1
    Perfect solution for people wondering if they should use MON or SUN to get week start, thank you! –  Aug 08 '16 at 06:01
  • I know this is old, but I'm using Java 8 and those methods definitely do NOT exist anymore. – Zephyr Aug 25 '17 at 21:41
  • @Zephyr Those are not core Java methods: they are part of the Joda-Time library. See [this answer](https://stackoverflow.com/a/33591149/1288408) for a solution using the new Java 8 date framework. Look for `firstOfWeek`. – Modus Tollens Aug 25 '17 at 21:56
13

Another option is to use roundFloorCopy. This looks like the following:

LocalDate startOfWeek = new LocalDate().weekOfWeekyear().roundFloorCopy();

For the last day of the standard week (Sunday) use roundCeilingCopy and minusDays

LocalDate lastDateOfWeek = new LocalDate().weekOfWeekyear().roundCeilingCopy().minusDays( 1 );

Also works for DateTime. And works for end of week (exclusive).

DateTime dateTime = new DateTime();
DateTime startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy();
DateTime endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy();

Dump to console…

System.out.println( "dateTime " + dateTime );
System.out.println( "startOfWeek " + startOfWeek );
System.out.println( "endOfWeek " + endOfWeek );

When run…

dateTime 2014-01-24T00:00:34.955-08:00
startOfWeek 2014-01-20T00:00:00.000-08:00
endOfWeek 2014-01-27T00:00:00.000-08:00
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
  • This code does not work for me. When I try `LocalDate startOfWeek = new LocalDate().dayOfWeek().roundFloorCopy();` or `LocalDate endOfWeek = new LocalDate().dayOfWeek().roundCeilingCopy();` I get today's date in both cases (a Thursday today). I'm using Joda-Time 2.3 and Java 8 beta 123. – Basil Bourque Jan 24 '14 at 03:42
  • @BasilBourque Thanks for pointing me to the error. `dayOfWeek` was the wrong field. With `weekOfWeekyear` it's working as supposed to be. – SpaceTrucker Jan 24 '14 at 07:21
  • Nice. That works with DateTime, even clearing the time-of-day to first moment. Is there a reason you used `LocalDate` in your code? – Basil Bourque Jan 24 '14 at 07:58
  • @BasilBourque No, this is just because the OP used 21/01/11 in his question, which is just a date without time. – SpaceTrucker Jan 24 '14 at 08:59
  • Good answer. Nice approach as well. – Madushan Perera Feb 06 '16 at 12:14
5

You can use the getDayOfWeek() method that gives you back 1 for Monday, 2 for Tue, .., 7 for Sunday in order to go back that many days and reach Monday:

import org.joda.time.DateTime;

    public class JodaTest {

        public static void main(String[] args) {
            DateTime date = new DateTime();
            System.out.println(date);
                    //2011-01-21T15:06:18.713Z
            System.out.println(date.minusDays(date.getDayOfWeek()-1));
                    //2011-01-17T15:06:18.713Z
        }
    }
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
4

See the section "Querying DateTimes" of the Joda-Time user guide.

Here is the general algorithm I would follow:

  1. find the day-of-week of the target date (Jan 21 2011 as you mentioned)
  2. determine how many days ahead of Monday this is
  3. Subtract the value of #2 from the target date using dateTime.minusDays(n)
matt b
  • 138,234
  • 66
  • 282
  • 345