Home > Net >  How to get the ceil and floor of a date time in java
How to get the ceil and floor of a date time in java

Time:11-04

if I have a date as 2022-10-26T08:05:00.123Z, how can I get the ceil and floor of it.

So how can I get this below expected output in java?

floor: 2022-10-26T00:00:00.000Z

ceil: 2022-10-27T00:00:00.000Z

I want to basically add a comparison if the given date lies between floor and ceil so that it will return all the dates that lies within them.

CodePudding user response:

To get the floor you can use the atStartOfDay() method of LocalDate:

LocalDate.now().atStartOfDay().toString()

To get the ceiling you can add 1 day then use the atStartOfDay() method again.

LocalDate.now().plusDays(1).atStartOfDay().toString()

CodePudding user response:

Try this:

  LocalDate floor = LocalDate.now();
  LocalDate ceil = floor.plusDays(1);
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
  System.out.println("floor: "   floor.atStartOfDay().format(formatter));
  System.out.println("ceil: "   ceil.atStartOfDay().format(formatter));

If your use case is just for checking that the date lies between ceil and floor, then formatting is not needed at all.

  • Related