How to round DateTime of Joda library to the nearest X minutes?

后端 未结 6 1557
[愿得一人]
[愿得一人] 2020-12-28 14:42

How to round DateTime of Joda library to the nearest X minutes ?
For example:

X = 10 minutes
Jun 27, 11:32 -> Jun 27, 11         


        
6条回答
  •  北海茫月
    2020-12-28 15:07

    Using pure DateTime (Joda) Java Library:

    DateTime dt = new DateTime(1385577373517L, DateTimeZone.UTC);
    // Prints 2013-11-27T18:36:13.517Z
    System.out.println(dt);
    
    // Prints 2013-11-27T18:36:00.000Z (Floor rounded to a minute)
    System.out.println(dt.minuteOfDay().roundFloorCopy());
    
    // Prints 2013-11-27T18:30:00.000Z (Rounded to custom minute Window)
    int windowMinutes = 10;
    System.out.println(
        dt.withMinuteOfHour((dt.getMinuteOfHour() / windowMinutes) * windowMinutes)
            .minuteOfDay().roundFloorCopy()
        );
    

提交回复
热议问题