Reputation: 17340
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
Upvotes: 3
Views: 21930
Reputation: 79085
java.time
In March 2014, Java 8 introduced the modern, java.time
date-time API which supplanted the error-prone legacy, java.util
date-time API. Any new code should use the java.time
API. If you are receiving an instance of java.util.Date
, convert it tojava.time.Instant
, using Date#toInstant
and derive other date-time classes of java.time
from it as per your requirement.
The first day of the week is locale-specific e.g.
DayOfWeek firstDowUk = WeekFields.of(Locale.UK).getFirstDayOfWeek();
DayOfWeek firstDowUs = WeekFields.of(Locale.US).getFirstDayOfWeek();
System.out.println(firstDowUk); // MONDAY
System.out.println(firstDowUs); // SUNDAY
So, the date on the first day of the week will also differ e.g.
// ZoneId.systemDefault() returns the ZoneId set to the JVM executing the code
// Change it as per your requirement e.g. ZoneId.of("Europe/London")
LocalDate today = LocalDate.now(ZoneId.systemDefault());
LocalDate dateOnFirstDowUk = today.with(TemporalAdjusters.previousOrSame(firstDowUk));
LocalDate dateOnFirstDowUs = today.with(TemporalAdjusters.previousOrSame(firstDowUs));
System.out.println(dateOnFirstDowUk);
System.out.println(dateOnFirstDowUs);
Use LocalDate#minusWeeks
to go back to a date by specified number of weeks e.g.
// Go back by one week
System.out.println(dateOnFirstDowUk.minusWeeks(1));
System.out.println(dateOnFirstDowUs.minusWeeks(1));
Demo:
public class Main {
public static void main(String[] args) {
DayOfWeek firstDowUk = WeekFields.of(Locale.UK).getFirstDayOfWeek();
DayOfWeek firstDowUs = WeekFields.of(Locale.US).getFirstDayOfWeek();
System.out.println(firstDowUk); // MONDAY
System.out.println(firstDowUs); // SUNDAY
// ZoneId.systemDefault() returns the ZoneId set to the JVM executing the code
// Change it as per your requirement e.g. ZoneId.of("Europe/London")
LocalDate today = LocalDate.now(ZoneId.systemDefault());
LocalDate dateOnFirstDowUk = today.with(TemporalAdjusters.previousOrSame(firstDowUk));
LocalDate dateOnFirstDowUs = today.with(TemporalAdjusters.previousOrSame(firstDowUs));
System.out.println(dateOnFirstDowUk);
System.out.println(dateOnFirstDowUs);
// Go back by one week
System.out.println(dateOnFirstDowUk.minusWeeks(1));
System.out.println(dateOnFirstDowUs.minusWeeks(1));
}
}
Learn more about the modern Date-Time API from Trail: Date Time.
Upvotes: 1
Reputation: 30077
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));
}
Upvotes: 2
Reputation: 51
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.
Upvotes: 4
Reputation: 120516
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.
Upvotes: 2