最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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 :)

Post a comment

comment list (0)

  1. No comments so far