0

I would like to get day of week from day of month.

For example:

I have a date: 2014-09-10 13:45:20

I parse this and get the day of month which is 10.

Now i would like to know, that the 10. (tenth) day of the 9. month (September) in the year of 2014 is which day of the week?

Just like Calendar.getInstance().get(Calendar.DAY_OF_WEEK); which is returns 1 for Monday, 2 for Tuesday, 3 for Wednesday... etc

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • 1
    Confusing question. Are you trying to print, e.g. `Wednesday` for a given input date? Or do you want a method that takes `int year, int month, int day` and gives a string value of the day name? – Duncan Jones Sep 10 '14 at 11:26
  • I just want to know which day is it in the given input date. – Adam Varhegyi Sep 10 '14 at 11:33
  • 1
    That's not an adequate description. Please spell out clearly what your input is (including type) and what your expected output is. – Duncan Jones Sep 10 '14 at 11:36
  • FYI, 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 20 '18 at 04:48

1 Answers1

4

Just call calendar.setTime(date); for your Calendar instance and Date object parsed from string:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = dateFormat.parse("2014-09-10 13:45:20");
System.out.println(date);

final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY);

Prints:

Wed Sep 10 13:45:20 MSK 2014
4
true