KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Let's assume that I have two dates like the following. DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata")); DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45"); DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45"); I want to compare these two dates to see whether firstDate is sooner, later or equal to secondDate . I could try the following. System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate)); System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate)); System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate)); What is produced by this code is exactly what I want. It produces the following output. firstDate = 2012-02-16T12:03:45.000+05:30 secondDate = 2013-02-17T12:03:45.000+05:30 comparison = true firstDate = 2012-02-16T12:03:45.000+05:30 secondDate = 2013-02-17T12:03:45.000+05:30 comparison = false firstDate = 2012-02-16T12:03:45.000+05:30 secondDate = 2013-02-17T12:03:45.000+05:30 comparison = false I need to ignore the seconds and the milliseconds portion of these dates. I have tried to use the withSecondOfMinute(0) and withMillis(0) methods like the following. DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata")); DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0); DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0); But it yielded the following output. fi
Tags (comma-separated)
Save Edits
Cancel