1

I am currently trying to schedule a method for execution once per week on a day which the user will select. I know I can get the current date via:

Date date = new Date();

When setting up my TimerTask for execution, I need to increment the date by 1-6 days depending on which day of week is selected by the user. I do not see a setDay() method in the documentation and was wondering if parsing the day out, changing it, and adding back to the date object is the only way. Seems like something much more simple would be out there.

GregH
  • 5,125
  • 8
  • 55
  • 109
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 07 '19 at 22:17

5 Answers5

3

You need to use a Calendar.

The java.util.Calendar class is an abstract encapsulation of the Date object.

Calendar provides getter and setter for the date fields.


Updated to an example of incrementing the day of the week as requested:
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,(calendar.get(Calendar.DAY_OF_WEEK)+1));
        //alternative:
        //calendar.add(Calendar.DAY_OF_WEEK, 1); 
        Date newDate = calendar.getTime();


Update: Note the java 8+ implementation using java.time

Calendar and Date have not been deprecated, you can still mix and match.

However if you want to handle time zones properly or want to do more localisation (when do you not?) then you are better off using java.time.

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
  • can you post an example of getting a date from a calendar object and incrementing the day of week? Needs to end up as a date object so I can use timer.schedule(... , ....) – GregH May 26 '15 at 14:23
  • I updated the answer with an example of incrementing the day of the week. – Laurentiu L. May 26 '15 at 14:30
  • When using calendar.set(Calendar.DAY_OF_WEEK, 2), it will set the calendar date to the previous monday whereas I need it to always be the coming day-of-week. Any idea around this? – GregH May 26 '15 at 14:56
  • First of all, please use constants i.e. calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY) . It will be easier to test a day of the week to a range ( calendar.get(Calendar.DAY_OF_WEEK)>=Calendar.MONDAY) and it is the proper way to use the API. Next please detail your problem with the date your using and the result you are expecting. – Laurentiu L. May 26 '15 at 15:03
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 07 '19 at 22:17
1
public static Date addDays(Date date, int days) {
    GregorianCalendar calendar = getCalendar(date);
    calendar.add(Calendar.DATE, days);
    return calendar.getTime();
}

This should do the trick.

quiqon
  • 49
  • 5
0

You probably want to use Calendar.

With a Calendar object, you can simply use Calendar.add(Calendar.DAY_OF_WEEK, 1); to add a single day

Dragondraikk
  • 1,659
  • 11
  • 21
0

It would be easy with Calendar class.

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1); // this will add number of days to current
                                // date
    Date date = cal.getTime(); // it will return the date object
    System.out.println(date);
K139
  • 3,654
  • 13
  • 17
0

You may be looking for Date.setDate().

Bear in mind that it's deprecated, and the docs recommend using Calendar.set(Calendar.DAY_OF_MONTH, int date).

Armand
  • 23,463
  • 20
  • 90
  • 119