I am trying to calculate the Date object of the last Monday at 12 am. Here is my code:
Calendar calendar = Calendar.getInstance()
calendar.setTime(currentDate)
Integer dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
if (dayOfWeek > Calendar.MONDAY) {
Integer daysDifferenceFromMonday = dayOfWeek - Calendar.MONDAY
calendar.add(Calendar.DATE, -daysDifferenceFromMonday)
} else if (dayOfWeek < Calendar.MONDAY) {
// it means that we are on Sunday and we need last sunday
Integer daysDifferenceFromMonday = 7 - (Calendar.MONDAY - dayOfWeek)
calendar.add(Calendar.DATE, -daysDifferenceFromMonday)
}
calendar.set(Calendar.MILLISECOND, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.HOUR, 0)
Date toDate = calendar.getTime()
As you can see, I am setting the hour to 0. But, calendar.getTime()
gives me 12:00:00.
Here is the debugger screenshot.
What am I doing wrong? This thing is very strait-forward.
I am trying to calculate the Date object of the last Monday at 12 am. Here is my code:
Calendar calendar = Calendar.getInstance()
calendar.setTime(currentDate)
Integer dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
if (dayOfWeek > Calendar.MONDAY) {
Integer daysDifferenceFromMonday = dayOfWeek - Calendar.MONDAY
calendar.add(Calendar.DATE, -daysDifferenceFromMonday)
} else if (dayOfWeek < Calendar.MONDAY) {
// it means that we are on Sunday and we need last sunday
Integer daysDifferenceFromMonday = 7 - (Calendar.MONDAY - dayOfWeek)
calendar.add(Calendar.DATE, -daysDifferenceFromMonday)
}
calendar.set(Calendar.MILLISECOND, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.HOUR, 0)
Date toDate = calendar.getTime()
As you can see, I am setting the hour to 0. But, calendar.getTime()
gives me 12:00:00.
Here is the debugger screenshot.
What am I doing wrong? This thing is very strait-forward.
Share Improve this question edited Nov 18, 2024 at 23:33 user85421 29.8k11 gold badges66 silver badges94 bronze badges asked Nov 18, 2024 at 23:09 Alex A.Alex A. 2,6234 gold badges41 silver badges83 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 3Just in case anyone is interested in the solution. I ended up doing something like this:
LocalDateTime rangeEnd = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay()
LocalDateTime rangeStart = rangeEnd.minusWeeks(1)
[
from: DateUtils.localDateTimeToDate(rangeStart),
to: DateUtils.localDateTimeToDate(rangeEnd)
]
Basically, a single line answers the original question :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745589556a4634750.html
Date
andCalendar
, outdated in Java 8 (10 years ago) - better use classes from thejava.time
package -- and posted code is not valid Java ?!? – user85421 Commented Nov 18, 2024 at 23:12HOUR_OF_DAY
is better thanHOUR
(unless also [re-]settingAM_PM
, otherwise you risk it beingPM
) (( but I would still prefer the newerjava.time
classes )) – user85421 Commented Nov 18, 2024 at 23:24LocalDate lastMonday = LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.MONDAY));System.out.println("Start of last Monday was: " +lastMonday.atStartOfDay());
– g00se Commented Nov 18, 2024 at 23:27