Comparing two Joda-Time DateTime objects

isAfter and isBefore methods compare dates by millis (ignoring time zone). In your example, the dates have equal millis. System.out.println(londonDT.getMillis() == estDT.getMillis()); will print true. Expressions londonDT.isBefore(estDT) londonDT.isAfter(estDT) are equal to londonDT.getMillis() < estDT.getMillis() londonDT.getMillis() > estDT.getMillis()

Joda DateTime to Unix DateTime

Any object that inherits from BaseDateTime (including DateTime) has the method public long getMillis() According to the API it: Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z. So a working example to get the seconds would simply be: new DateTime().getMillis() / 1000 For completeness, the definition of the Unix Timestamp … Read more

How to get properly current date and time in Joda-Time?

Here is pseudo Code for Joda Time which could be useful to you. import org.joda.time.*; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class JodaTimeExample { public static void main(String[] sm) { DateTimeFormatter dateFormat = DateTimeFormat .forPattern(“G,C,Y,x,w,e,E,Y,D,M,d,a,K,h,H,k,m,s,S,z,Z”); String dob = “2002-01-15”; LocalTime localTime = new LocalTime(); LocalDate localDate = new LocalDate(); DateTime dateTime = new DateTime(); LocalDateTime localDateTime … Read more

ISO 8601 Time Interval Parsing in Java

java.time The java.time framework built into Java 8 and later has a Duration.parse method for parsing an ISO 8601 formatted duration: java.time.Duration d = java.time.Duration.parse(“PT1H2M34S”); System.out.println(“Duration in seconds: ” + d.get(java.time.temporal.ChronoUnit.SECONDS)); Prints Duration in seconds: 3754

Is there a standard implementation for a GSON Joda Time serialiser?

I’ve decided to roll my own open source one – you can find it here: https://github.com/gkopff/gson-jodatime-serialisers Here’s the Maven details (check central for the latest version): <dependency> <groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId> <artifactId>gson-jodatime-serialisers</artifactId> <version>1.6.0</version> </dependency> And here’s a quick example of how you drive it: Gson gson = Converters.registerDateTime(new GsonBuilder()).create(); SomeContainerObject original = new SomeContainerObject(new DateTime()); String json = … Read more

Parse Date String to Some Java Object

Using Joda-Time, take a look at DateTimeFormat; it allows parsing both kind of date strings that you mention (and almost any other arbitrary formats). If your needs are even more complex, try DateTimeFormatterBuilder. To parse #1: DateTimeFormatter f = DateTimeFormat.forPattern(“yyyy-MM-dd HH:mm:ss”); DateTime dateTime = f.parseDateTime(“2012-01-10 23:13:26”); Edit: actually LocalDateTime is a more appropriate type for … Read more