TL; DR
Collections.reverse( new ArrayList<>().add( OffsetDateTime.parse( "Thu Dec 27 11:02:43 GMT+05:30 2012" , DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss OOOO uuuu" , Locale.US ) ) ) ) ( Collections.reverse( new ArrayList<>().add( OffsetDateTime.parse( "Thu Dec 27 11:02:43 GMT+05:30 2012" , DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss OOOO uuuu" , Locale.US ) ) ) ) mm: ss OOOO uuuu", Locale.US) Collections.reverse( new ArrayList<>().add( OffsetDateTime.parse( "Thu Dec 27 11:02:43 GMT+05:30 2012" , DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss OOOO uuuu" , Locale.US ) ) ) )
java.time
In the modern approach uses java.time classes.
Define a formatting template to match. By the way, this is a terrible format; if you have any kind of control, use the standard ISO 8601 .
String input = "Thu Dec 27 11:02:43 GMT+05:30 2012" ; DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss OOOO uuuu" , Locale.US );
Your input lines indicate the offset-from-UTC, but not a full time zone. So, we analyze how OffsetDateTime .
OffsetDateTime odt = OffsetDateTime.parse( input , f );
odt.toString (): 2012-12-27T11: 02: 43 + 05: 30
OffsetDateTime objects already know how to sort themselves , as they implement Comparable .
List< OffsetDateTime > odts = new ArrayList<>( 3 ) ; odts.add( odt ) ; odts.add( odt.plusMinutes( 7 ) ) ; odts.add( odt.minusMinutes( 21 ) ) ; Collections.sort( odts ) ; <> ( List< OffsetDateTime > odts = new ArrayList<>( 3 ) ; odts.add( odt ) ; odts.add( odt.plusMinutes( 7 ) ) ; odts.add( odt.minusMinutes( 21 ) ) ; Collections.sort( odts ) ;
You want to use the latter at the top of the list, so the sort in reverse order .
Collections.reverse( odts ) ;
To compare individual objects java.time, call isBefore , isAfter and isEqual / equals .
thisOdt.isBefore( thatOdt )
About java.time
The java.time framework is built into Java 8 and later. These classes displace unpleasant old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
Project Joda-Time The , now the maintenance mode , we recommend you go to classes java.time .
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?