How to sort dates from the current date to the old Android or Java?

I need a user-defined function that can sort by the date the current time to the old days.

I have a list of 10 dates that I want to sort by date, starting from the most recent recent date.

At the moment I have a logic in which, if we can hide the date in milliseconds, then compare it with the current millisecond milliseconds and the lower will be the last date. I.e

CURRENT_MILLI_SECOND - A_DATE_CONVERTED_TO_MILLI_SECONDS = MILLI-SECONDS 

Please suggest me if anyone can help me in this logic or any other logic ... !!!

This is a format that I get from the server:

 Thu Dec 27 11:02:43 GMT+05:30 2012 
+4
source share
5 answers

You can use comparator , and you can sort the data using the compare ()

 Collections.sort(dateList, new Comparator<Date>(){ public int compare(Date date1, Date date2){ return date1.after(date2); } }); date2) { Collections.sort(dateList, new Comparator<Date>(){ public int compare(Date date1, Date date2){ return date1.after(date2); } }); 
+6
source

Try the following:

  Comparator date_comparator = new Comparator() { @Override public int compare(Date date1, Date date2){ return date1.compareTo(date2); } }; date2) {  Comparator date_comparator = new Comparator() { @Override public int compare(Date date1, Date date2){ return date1.compareTo(date2); } }; 
+5
source

For this you can use a class Calendar or Date . Using the calendar date, you can compare two dates, such as date1.compare (date2) or date.before (date2) or date1.after (date2) api are available for your events.

+1
source

Date and Calendar classes in Java already has a good API for comparing dates.

You can use this to write your own function to sort dates.

You can refer to this link .

0
source

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 ) ; // Reverse-order. 

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?

0
source

All Articles