admin管理员组文章数量:1023815
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 :)
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-
4
"What am I doing wrong?" - using these outdated classes like
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:12 -
1
and maybe using
HOUR_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:24 -
3
You should be doing something like:
LocalDate 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 - 1 @user85421 It didn't stop, where it stopped is simply no in the screenshot. HOUR_OF_DAY helped. Thank you – Alex A. Commented Nov 18, 2024 at 23:32
- 1 @g00se OOOOHhhhh!!! This is awesome!! Thank you! – Alex A. Commented Nov 19, 2024 at 1:16
1 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 :)
本文标签: javaTrying to get the datetime of 12am on last Mondayit always gives me 12pmStack Overflow
版权声明:本文标题:java - Trying to get the datetime of 12am on last Monday - it always gives me 12pm - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745589554a2157806.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