Get first day of a particular week in Joda-Time? java

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

Java Calendar using Calendar.DAY_OF_WEEK to get the first and the last dates for a particular date

In my application there lies a code which works abruptly sometimes, its about getting a week interval using the java calendar object through Calendar.DAY_OF_WEEK. The code checked for monday as start

Get localized week number with JodaTime

I am trying to get the current week number with JodaTime. In France, weeks are defined like this : A week begins on Monday (whereas weeks begin on Sunday in the US). The first week of the year is the

Get first day of week in PHP?

Given a date MM-dd-yyyy format, can someone help me get the first day of the week?

Cocoa get first day in week

How to get the first day of a week for a date this seems more easy at it is, since : when the week starts with sunday, i need to get back the sunday date if it starts on monday, i need to get the mon

JodaTime – first day of next weekend

In JodaTime, it is possible to calculate the first day of next weekend ? For example, now is 19.05.2014 (Monday) and first day of next weekend is Saturday 24.05.2014.

Get First Day Of Week From Week Number

In Oracle, is there a straightforward way to get the first day of the week given a week number? For example, today’s date is 12/4/2012. If I run: select to_char(sysdate,’WW’) from dual; It returns 49

How to get first day of week in c

I am coding little calendar program in c. I have issue to get first day of week in current locale, i need it to format calendar. For example Sunday – is first day of week in US, but in Europe first da

How to get first day of the week and last day of the week in sql server 2008?

How to get the first day of the week and last day of the week when we input any one day of a week? For example if we enter a date then the first(Monday) and last (Friday) day should be displayed. that

Get the first day and last day of a week

I am using this method to get the current week first and last days: NSDate *weekDate = [NSDate date]; NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDa

Get date of first and last day of week knowing week number

I need to get the date of the first and last day of the week knowing the week number. I get a start date and an end date, representing the first and last day of a selected week in a given year. then I

Answers

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
        }
    }

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)

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
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

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