learn java
learn java

Reputation: 27

how to identify the week from current date in java

I want to identity the whole week date(From Sunday To Saturday) using current date in java.

For Example: Today is Tuesday - it means I need dates for Tuesday, Monday and Sunday.

If current day is Wednesday - then I need dates from Sunday to Wednesday.

How to achieve this logic in java?

I'm able to get the start of week from current date, but I don't know how to get the remaining day date from Week start date. Is there any java utility available for this?

Upvotes: 0

Views: 1105

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347334

For reference, the current date used to formulate the output was Wednesday, 22nd July, 2015 (22/07/2015)

Java 8

LocalDate ld = LocalDate.now();
LocalDate sunday = ld.minusDays(ld.getDayOfWeek().getValue());
LocalDate tommorrow = ld.plusDays(1);
LocalDate date = sunday;
while (date.isBefore(tommorrow)) {
    System.out.println(date);
    date = date.plusDays(1);
}

Prints

2015-07-19
2015-07-20
2015-07-21
2015-07-22

As an alternative

(Which will basically work for all the other mentioned APIs) you could simply walk backwards from today...

LocalDate date = LocalDate.now();
do {
    System.out.println(date);
    date = date.minusDays(1);
} while (date.getDayOfWeek() != DayOfWeek.SATURDAY);

Prints

2015-07-22
2015-07-21
2015-07-20
2015-07-19

JodaTime

LocalDate now = LocalDate.now();
LocalDate sunday = now.minusDays(5).withDayOfWeek(DateTimeConstants.SUNDAY);
LocalDate tommorrow = now.plusDays(1);
LocalDate date = sunday;
while (date.isBefore(tommorrow)) {
    System.out.println(date);
    date = date.plusDays(1);
}

Prints

2015-07-19
2015-07-20
2015-07-21
2015-07-22

Calendar

As a last resort. Remember though, Calendar carries time information, so using before, after and equals may not always do what you think they should...

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

Calendar today = Calendar.getInstance();

while (cal.before(today)) {
    System.out.println(cal.getTime());
    cal.add(Calendar.DATE, 1);
}

Prints

Sun Jul 19 15:01:49 EST 2015
Mon Jul 20 15:01:49 EST 2015
Tue Jul 21 15:01:49 EST 2015
Wed Jul 22 15:01:49 EST 2015

Upvotes: 7

Edward J Beckett
Edward J Beckett

Reputation: 5140

This is pretty easy using Java 8. However, note, the iso-8601 standard represents Monday as the first day of the week... See the JavaDocs... so to implement your requirements you simply find Monday as the base case...

DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). It is recommended that applications use the enum rather than the int value to ensure code clarity.

/**
* @author Edward Beckett :: <[email protected]>
* @since :: 7/22/2015
*/
public class WeekOfToday{

public static void main( String[] args ) {

    Integer dayOfWeek = LocalDate.now().getDayOfWeek().compareTo( DayOfWeek.MONDAY ) ;
    LocalDate startOfWeek =  LocalDate.now().minusDays( dayOfWeek );
    LocalDate endOfWeek = LocalDate.now().plusDays( DayOfWeek.SUNDAY.getValue() - dayOfWeek - 1 );
    }
}

output

Start of Week : 2015-07-20
End of Week : 2015-07-26

Upvotes: 0

mustangDC
mustangDC

Reputation: 967

I want to identity the whole week date(From Sunday To Saturday) using current date in java.

This should solve your problem, you can further modify according to your situation.

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("Date " + c.getTime());
c.add(Calendar.DATE, +1);
System.out.println(c.getTime());

Hope it helps

Upvotes: 0

Related Questions