How do you get the date/time range for “today” using the Joda date/time library in Java?

后端 未结 6 1705
故里飘歌
故里飘歌 2021-01-17 07:59

Assuming this is how you get the current time in Joda time:

DateTime now = new DateTime();

How do you calculate values for the variables

6条回答
  •  逝去的感伤
    2021-01-17 08:15

    I would use:

    LocalDate today = now.toLocalDate();
    LocalDate tomorrow = today.plusDays(1);
    
    DateTime startOfToday = today.toDateTimeAtStartOfDay(now.getZone());
    DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(now.getZone());
    

    Then check if startOfToday <= time < startOfTomorrow for any particular time.

    Of course, it partly depends on exactly what's stored in the database - and what time zone you're interested in.

提交回复
热议问题