0

In Java Calendar exists method set. E.g. now is 01/11/2011. If we make like:

Calendar now = new GregorianCalendar();

now.set(GregorianCalendar.DAY_OF_WEEK,Calendar.FRIDAY);

Calendar will be set to nearest Friday, but for date more than now. In docs written nothing if always go to the greater date. Anybody can confirm that? Thanks.

user710818
  • 23,228
  • 58
  • 149
  • 207

3 Answers3

3

It always sets it to the given day-of-week within the same week. The first day of the week will depend on the calendar and locale.

For example, using the default calendar on my machine in the UK, Monday is the first day of the week - so setting the day of week to Monday today results in October 31st, whereas setting the day of week to Sunday results in November 6th. However, if I use:

Calendar now = Calendar.getInstance(Locale.US);
now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println(now.getTime());

... then the first day of the week is Sunday, and the result is October 30th.

You can set the first day of the week explicitly using setFirstDayOfWeek.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // Replace JVM's ZoneId, ZoneId.systemDefault() with the applicable one e.g.
        // ZoneId.of("America/Los_Angeles")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        // Next Sunday
        LocalDate nextSun = today.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(nextSun);

        // Same (if it's Sunday today) of next Sunday
        LocalDate sameOrNextSun = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        System.out.println(sameOrNextSun);

        // Previous Sunday
        LocalDate previousSun = today.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));
        System.out.println(previousSun);

        // Same (if it's Sunday today) of previous Sunday
        LocalDate sameOrPreviousSun = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
        System.out.println(sameOrPreviousSun);
    }
}

Output from a sample run:

2021-07-25
2021-07-18
2021-07-11
2021-07-18

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

That method will set the calendar to be Friday of 'this' week. Whether that's in the past or future depends on where in the current week you are at the moment. It is also a locale dependant call, since the first day of the week can be different depending where you are.

Affe
  • 47,174
  • 11
  • 83
  • 83