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

后端 未结 6 1699
故里飘歌
故里飘歌 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:37

    This works better, it turns out DateTime has a method called toInterval which does this exact thing (figures out midnight to midnight). In my tests, it appears to have no problem with DST transitions.

    DateTime now = new DateTime();
    DateTime startOfToday = now.toDateMidnight().toInterval().getStart();
    DateTime endOfToday = now.toDateMidnight().toInterval().getEnd();
    System.out.println( "\n" + now + "\n" + startOfToday + "\n" + endOfToday + "\n" );
    

    JODA looks to be very well thought out.

提交回复
热议问题