Comparing two dates using Joda time
System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight())); or System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));
System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight())); or System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));
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()
This can happen in the case you miss initialization: JodaTimeAndroid.init(this);
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
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
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
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
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
I was able to get the answer to this from the Jackson user mailing list, and wanted to share with you since it is a newbie issue. From reading the Jackson Date FAQ, I did not realize that extra dependencies and registration are required, but that is the case. It is documented at the git … Read more
Try LocalDate.withDayOfWeek: LocalDate now = new LocalDate(); System.out.println(now.withDayOfWeek(DateTimeConstants.MONDAY)); //prints 2011-01-17 System.out.println(now.withDayOfWeek(DateTimeConstants.SUNDAY)); //prints 2011-01-23