-2

I'm setting time using Timestamp class with current time. I'm setting the time with the Calendar class first by DAY_OF_WEEK and second with DAY_OF_MONTH. I’m getting the same output every time. Then what is the diffrence between DAY_OF_MONTH and DAY_OF_WEEK?

    Timestamp followUpDateBegins = new Timestamp(System.currentTimeMillis());

    Calendar cal = Calendar.getInstance();
    cal.setTime(followUpDateBegins);
    cal.add(Calendar.DAY_OF_WEEK, -30);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    followUpDateBegins.setTime(cal.getTime().getTime());
    System.out.println("followUpDateBegins      "+followUpDateBegins);

OR

    Timestamp followUpDateBeginsSecond = new Timestamp(System.currentTimeMillis());

    cal.setTime(followUpDateBeginsSecond);
    cal.add(Calendar.DAY_OF_MONTH, -30);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    followUpDateBeginsSecond.setTime(cal.getTime().getTime());
    System.out.println("followUpDateBegins" + followUpDateBeginsSecond);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Parminder Singh
  • 131
  • 3
  • 10
  • 1
    What's the difference? They are completely different things. The day of the month today is 10. The day of the week today is Tuesday, which is represented in `Calendar` by [`TUESDAY`](https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#TUESDAY), the number 3. – khelwood Apr 10 '18 at 09:20
  • It might not make a difference in your case when you use the `add` method on an existing date, but it certainly would if you used the `set` method. – OH GOD SPIDERS Apr 10 '18 at 09:21
  • 1
    Why do you want to bother? Both `Timestamp` and `Calendar` are long outdated, and today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And the modern classes can be saved to your SQL database (through JDBC or JPA, for example) just like we did with `Timestamp` in the old days. – Ole V.V. Apr 10 '18 at 09:30
  • 1
    @OleV.V.sometimes, unfortunately, it's not up to you, even if you're right... working with GWT for instance, i don't event **have** the Calendar API, juste `Date`! – HBo Apr 10 '18 at 09:33
  • First try the [JAVADOC](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html) and then something simple like `System.out.println("week: " + cal.get(DAY_OF_WEEK) + ", month: " + cal.get(DAY_OF_MONTH));` – user85421 Apr 10 '18 at 09:46
  • 1
    As Ole V.V. commented, the troublesome old 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 Apr 10 '18 at 20:34

3 Answers3

5

DAY_OF_WEEK is the day of the week (7 days), DAY_OF_MONTH is the day of the month (<=31 days)

HBo
  • 635
  • 6
  • 16
  • No problem. Don't hesitate to read the `Calendar` Javadoc, I find it very understandable and comprehensive. Even though it's quite and outdated API, it's still quite powerful and easy to handle. – HBo Apr 10 '18 at 09:41
4

If you use add

cal.add(Calendar.DAY_OF_WEEK, -30); is the same as cal.add(Calendar.DAY_OF_MONTH, -30);

Also DATE and DAY_OF_YEAR will act the same - because you modify Days in both cases and you just go 30 days behind.

But if your calendar is set to 15th of April for example and you do cal.get(Calendar.DAY_OF_MONTH) and cal.get(Calendar.DAY_OF_WEEK) they will return different results.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
2

As explained in the other answers, adding both fields results in the same date, which is a bizarre implementation detail, IMO. We can see that from the source code, there's a switch statement where both fields are considered "the same" when adding:

case DAY_OF_MONTH: // synonym of DATE
case DAY_OF_YEAR:
case DAY_OF_WEEK:

Alghough it doesn't make sense "adding days of week" (IMHO - because in the end you just add days, no matter if it's in the same week, month or whatever, and that's it), that's the way it's implemented. This API is really confusing...

Another detail is that Calendar.getInstance() already returns the current date/time - it internally calls System.currentTimeMillis(), so creating a Timestamp just to set it in the calendar is redundant. You could just do:

// create the calendar
Calendar cal = Calendar.getInstance();

// do your stuff
...

// create the timestamp
Timestamp followUpDateBeginsSecond = new Timestamp(cal.getTimeInMillis());
paulXkt
  • 93
  • 4