Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jodatime

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

like image 329
Jonathan Avatar asked Jan 21 '11 14:01

Jonathan


2 Answers

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
like image 172
dogbane Avatar answered Oct 18 '22 20:10

dogbane


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

like image 27
sudhir Avatar answered Oct 18 '22 19:10

sudhir