Java library to operate with time intervals.
Supported intervals are:
- Custom interval with arbitrary start/end time
- Year
- Month
- Day
<dependency>
<groupId>ws.slink</groupId>
<artifactId>intervals4j-core</artifactId>
<version>0.0.2</version>
</dependency>- custom interval
Interval interval = new IntervalBuilder()
.timezone(TimeZone.getTimeZone("UTC"))
.start(LocalDateTime.of(2023, 10, 15, 10, 15, 0, 0))
.end(LocalDateTime.of(2023, 10, 15, 16, 15, 0, 0))
.build();
System.out.println(interval);- month interval
// create interval of October 2023 in "India Standard Time" timezone
Month month = IntervalBuilder.month(2023, 10, "IST");
// prints:
// 2023-10-01 00:00:00.000 - 2023-10-31 23:59:59.999 IST - month interval with offset
offset allows to shift original interval bounds
// create interval of October 2023 in "India Standard Time" timezone
Month monthShiftPlus10 = IntervalBuilder.month(2023, 10, "IST", 10);
System.out.println(monthShiftPlus10);
// prints:
// 2023-10-01 10:00:00.000 - 2023-11-01 09:59:59.999 IST
// create interval of October 2023 in "India Standard Time" timezone
Month monthShiftMinus10 = IntervalBuilder.month(2023, 10, "IST", -10);
System.out.println(monthShiftMinus10);
// prints:
// 2023-09-30 14:00:00.000 - 2023-10-31 13:59:59.999 IST- year interval
Year yearInUtc = IntervalBuilder.year(2023);
System.out.println(yearInUtc);
// prints:
// 2023-01-01 00:00:00.000 - 2023-12-31 23:59:59.999 UTC- day interval
Day dayInUtcShifted = IntervalBuilder.day(2023, 10, 15, -3);
System.out.println(dayInUtcShifted);
// prints:
// 2023-10-14 21:00:00.000 - 2023-10-15 20:59:59.999 UTC(only generic Interval can be received as a result of parsing procedure)
Interval interval = IntervalBuilder.parse("2023");
System.out.println(interval);
// prints:
// 2023-01-01 00:00:00.000 - 2023-12-31 23:59:59.999 UTC- parse with timezone
Interval interval = IntervalBuilder.parse("2023-10", "IST");
System.out.println(interval);
// prints:
// 2023-10-01 00:00:00.000 - 2023-10-31 23:59:59.999 IST- parse with offset
Interval interval = IntervalBuilder.parse("2023-10-15", 10);
System.out.println(interval);
// prints:
// 2023-10-15 10:00:00.000 - 2023-10-16 09:59:59.999 UTC- parse with offset and timezone
Interval interval = IntervalBuilder.parse("2023-10-15", "IST", 10);
System.out.println(interval);
// prints:
// 2023-10-15 10:00:00.000 - 2023-10-16 09:59:59.999 IST Interval interval = IntervalBuilder.parse("2023-10-15", "IST", 10);
Interval check = IntervalBuilder.parse("2023-10-16", "IST");
System.out.println(interval.contains(check.start()));
System.out.println(interval.contains(check.end()));
// prints:
// true
// falseFor serialization purposes one can use intervals4j serialization libarary (based on Jackson)
<dependency>
<groupId>ws.slink</groupId>
<artifactId>intervals4j-jackson</artifactId>
<version>0.0.2</version>
</dependency> // spring
@Configuration
public class ObjectMapperConfig {
@Bean
Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder
.serializerByType(Interval.class, new IntervalSerializer())
.deserializerByType(Interval.class, new IntervalDeserializer());
}
}
// plain java
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(
new SimpleModule()
.addSerializer(Interval.class, new IntervalSerializer())
.addDeserializer(Interval.class, new IntervalDeserializer())
);