Joda-Time: DateTime, DateMidnight and LocalDate usage

I see advantages in having almost all the Interfaces using LocalDateTime (at the Service Layer at least) so that my Application doesn’t have to manage Timezones and can safely assume Times always in UTC. I’m not sure I understand your line of thinking here. LocalDateTime and DateTime represent two quite different concepts. It’s not the … Read more

LocalDate interval in Joda-time

I personnaly use the Range class from Guava. It supports open ended ranges. It is also possible to specify included or excluded bounds. Among other numerous possibilities, those allow to easily represent “before a date” or “after a date”. Example for open-ended intervals. Range<LocalDate> before2010 = Range.atMost(new LocalDate(“2009-12-31”)); Range<LocalDate> alsoBefore2010 = Range.lessThan(new LocalDate(“2010-01-01”)); It also … Read more

JodaTime – how to get current time in UTC

You’re making it far more complicated than you need to: DateTime dt = new DateTime(DateTimeZone.UTC); No conversion required at all. If you find you actually need to convert, you can use withZone. I’d suggest you avoid going via LocalDateTime, however, as that way you can lose information due to time zone transitions (two different instants … Read more

In Joda-Time, set DateTime to start of month

Midnight at the start of the first day of the current month is given by: // first midnight in this month DateMidnight first = new DateMidnight().withDayOfMonth(1); // last midnight in this month DateMidnight last = first.plusMonths(1).minusDays(1); If starting from a java.util.Date, a different DateMidnight constructor is used: // first midnight in java.util.Date’s month DateMidnight first … Read more