Java Data Type How to - Get the date of the coming Wednesday








Question

We would like to know how to get the date of the coming Wednesday.

Answer

import java.util.Calendar;
// ww w .j av a  2s  .  c o  m
public class Main {
    public static Calendar nextDayOfWeek(int dow) {
        Calendar date = Calendar.getInstance();
        int diff = dow - date.get(Calendar.DAY_OF_WEEK);
        if (!(diff > 0)) {
            diff += 7;
        }
        date.add(Calendar.DAY_OF_MONTH, diff);
        return date;
    }
    public static void main(String[] args) {
        System.out.printf(
            "%ta, %<tb %<te, %<tY",
            nextDayOfWeek(Calendar.WEDNESDAY)
        );
    }
}

The code above generates the following result.