java - Trying to get the datetime of 12am on last Monday - it always gives me 12pm - Stack Overflow

I am trying to calculate the Date object of the last Monday at 12 am.Here is my code:Calendar calenda

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 and Calendar, outdated in Java 8 (10 years ago) - better use classes from the java.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 than HOUR (unless also [re-]setting AM_PM, otherwise you risk it being PM) (( but I would still prefer the newer java.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
 |  Show 4 more comments

1 Answer 1

Reset to default 3

Just 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信