How can I get the friday of the 34th week of the year? Not just the friday. Any day, I want to get the current week of the year, and in this week I want the friday. Thanks in advance
Asked
Active
Viewed 90 times
1 Answers
1
Use the Calendar
class:
Calendar c = Calendar.getInstance();
c.setWeekDate(2020, 34, Calendar.FRIDAY);
Then you can access the Date
object for this day like this:
System.out.println(c.getTime());
Which will print (for me): Fri Aug 21 15:45:22 CEST 2020
.
Or you can even access the individual calendar fields like so:
System.out.println(
"YEAR: " + c.get(Calendar.YEAR) + "\n" +
"MONTH: " + c.get(Calendar.MONTH) + "\n" +
"DAY: " + c.get(Calendar.DAY_OF_MONTH));
This will print:
YEAR: 2020
MONTH: 21
DAY: 7
Just don't make the mistake to try to achieve things like this purely with the help of Date
.

bkis
- 2,530
- 1
- 17
- 31
-
1Oh, look! A downvote without a comment! How nice! Does this somehow _not_ answer the question? – bkis Jan 12 '21 at 14:59
-
1Not sure why, but I think the downvote is because you are referring to old classes, instead of showing the newer Date/Time api's – Stultuske Jan 12 '21 at 15:12
-
-
2What's wrong with "old" classes if they can produce such a concise solution? – Ralf Kleberhoff Jan 12 '21 at 15:27