jodatime
Why dec 31 2010 returns 1 as week of year?
The definition of Week of Year is Locale dependent. How it is defined in US is discused in the other posts. For example in Germany (DIN 1355-1 / ISO 8601): the first Week* of Year is the first week with 4 or more days in the new year. *first day of week is Monday and … Read more
Understanding the Etc/GMT time zone
Etc/GMT is not strictly the same thing as UTC or GMT. They represent the same instant in time only when the offset is 0. In all other cases, they are quite different. Apple explains the designation here. A quote directly from the link gives an example: We use POSIX-style signs in the Zone names and … Read more
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
java.util.date to String using DateTimeFormatter
If you are using Java 8, you should not use java.util.Date in the first place (unless you receive the Date object from a library that you have no control over). In any case, you can convert a Date to a java.time.Instant using: Date date = …; Instant instant = date.toInstant(); Since you are only interested … Read more
Joda Time parse a date with timezone and retain that timezone
Basically, once you parse the date string [in your createDate() method] you’ve lost the original zone. Joda-Time will allow you to format the date using any zone, but you’ll need to retain the original zone. In your createDate() method, the DateTimeFormatter “df” can return the zone that was on the string. You’ll need to use … Read more
How do I create a new Joda DateTime truncated to the last hour? [duplicate]
Wohoo, found it. Simple like everything in Joda once I traced down the calls. DateTime dt = new DateTime().hourOfDay().roundFloorCopy();
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