3

I want to get the starting and ending dates of a week for example

2012-05-06 to 2012-05-12
2012-05-13 to 2012-05-19

The code I have written is

currWeekCalender.add(Calendar.WEEK_OF_YEAR, 1);

    String dateStart =  currWeekCalender.get(Calendar.YEAR) + "-" + addZero((currWeekCalender.get(Calendar.MONTH) + 1)) + "-" + addZero(currWeekCalender.getFirstDayOfWeek());
    currWeekCalender.add(Calendar.DAY_OF_MONTH,7);
    String dateEnd =  currWeekCalender.get(Calendar.YEAR) + "-" + addZero((currWeekCalender.get(Calendar.MONTH) + 1)) + "-" + addZero(currWeekCalender.get(Calendar.DAY_OF_MONTH));

but the results are not correct, also I want previous weeks date.

Thanks

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • 3
    What do you mean by "not correct"? What are you seeing, and what do you expect to see? Also, that's not how to get a String from a Date object. Use a SimpleDateFormat object for this. – Hovercraft Full Of Eels May 05 '12 at 20:01
  • @kaibuki can you get any solution becuase same thing i required.. – PankajAndroid Sep 10 '13 at 05:55
  • possible duplicate of [Java - How to calculate the first and last day of each week](http://stackoverflow.com/questions/3023267/java-how-to-calculate-the-first-and-last-day-of-each-week). And do some searching for other similar questions with helpful answers and example code, including all those "Related" items listed on the lower right (if viewing this in web browser). – Basil Bourque Oct 26 '14 at 08:17
  • Tip: For handling a span of time (a week), see the `Interval` class in Joda-Time and similar class in java.time package in Java 8. – Basil Bourque Oct 26 '14 at 08:23
  • See [java-how-to-calculate-the-first-and-last-day-of-each-week](https://stackoverflow.com/questions/3023267/java-how-to-calculate-the-first-and-last-day-of-each-week) Also take a look at [Joda Time](http://joda-time.sourceforge.net/userguide.html), which simplifies working with dates and times in Java. – Christopher Peisert May 05 '12 at 20:12

3 Answers3

4

Hello to all coders :)

I work on little app to dive some data from database. To calculate previous weeks start and end date i use this code:

// Calendar object
Calendar cal = Calendar.getInstance();

// "move" cal to monday this week (i understand it this way)
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

// calculate monday week ago (moves cal 7 days back)
cal.add(Calendar.DATE, -7);
Date firstDateOfPreviousWeek = cal.getTime();

// calculate sunday last week (moves cal 6 days fwd)
cal.add(Calendar.DATE, 6);
Date lastDateOfPreviousWeek = cal.getTime();

Hope, that helps.

ogniwo100
  • 51
  • 3
2

Your problem is that getFirstDayOfWeek() returns the first day of the week; e.g., Sunday in US, Monday in France. It does not return a day of the month. See javadoc.

The first day in a month that is the start of the week is (in pseudo-code)

((7 + (firstDayOfWeek - dayOfWeek(firstOfMonth))) % 7) + 1

You can translate that into java.util.Calendar code if you like, but I would suggest using Joda time instead.


also I want previous weeks date.

Just subtract seven days maybe using add

currCalendar.add(Calendar.DAY_OF_MONTH, -7)

This may involve underflow, but add deals with that.

add(f, delta)

adds delta to field f. This is equivalent to calling set(f, get(f) + delta) with two adjustments:

Add rule 1. The value of field f after the call minus the value of field f before the call is delta, modulo any overflow that has occurred in field f. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

Java 8 version

This prints previous 10 weeks

final ZonedDateTime input = ZonedDateTime.now();

for(int i = 1; i < 10; i++) {
    final ZonedDateTime startOfLastWeek = input.minusWeeks(i).with(DayOfWeek.MONDAY);
    System.out.print(startOfLastWeek.format(DateTimeFormatter.ISO_LOCAL_DATE));
    final ZonedDateTime endOfLastWeek = startOfLastWeek.plusDays(6);
    System.out.println(" - " + endOfLastWeek.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
Community
  • 1
  • 1
freedev
  • 25,946
  • 8
  • 108
  • 125