Java program to sort a list of date which is in format dd MMM yyyy

java sort list by date ascending
javascript sort date dd/mm/yyyy hh:mm:ss
how to sort date and time in java
sort list based on date in java 8
convert string to date and sort java
compare string dates java
compare list of dates in java
how to sort list by date in android

Input is a list containing date in String format. I have solution as below. But i feel it can be made more efficient. Any help would be appreciated.

//Map to store month data

HashMap<String,String> month = new HashMap<String,String>();
        month.put("Jan","01");
        month.put("Feb","02");
        month.put("Mar","03");
        month.put("Apr","04");
        month.put("May","05");
        month.put("Jun","06");
        month.put("Jul","07");
        month.put("Aug","08");
        month.put("Sep","09");
        month.put("Oct","10");
        month.put("Nov","11");
        month.put("Dec","12");

You can consider this as input

    String[] input = {"20 Oct 2052",
    "26 May 1960",
    "06 Jun 1933",
    "06 Jun 1933",
    "06 Jun 1933",
};


    ArrayList<Long> temp1 = new ArrayList<Long>();

To compare result

    HashMap<Long,String> temp2 = new HashMap<Long,String>();

    ArrayList<String> result = new ArrayList<String>();
    for(int i = 0 ; i< input.length ; i++){
        String j = "";
        if(input[i].length() == 11){

            j+= input[i].substring(7,11);
            j+= month.get(input[i].substring(3,6));
            j+=input[i].substring(0,2);

            temp1.add(Long.parseLong(j));
            temp2.put(Long.parseLong(j), input[i]);   
        }
    }

Sorting the result

Collections.sort(temp1);

Printing the result

System.out.println(temp1.toString());

Radix Sort is your friend. Just sort strings with this algorithm. This is optimal solution.

Java : Sort Date Time Object and Text Format, (dateStrList, new Comparator() { DateFormat f = new SimpleDateFormat("MM/dd/yyyy '@'hh:mm a"); @Override public int compare(String o1, String o2) { try { return f. parse(o1). Today's date and time = Mon, 26 Nov 2018 09:34:11 +0000 Current Date in MM-dd-yyyy hh:mm:ss format = 11-26-2018 09:34:11 Current Date = 26/November/2018 Current Time = 09.34.11 +0000 Samual Sam Published on 26-Dec-2018 16:09:58

Firstly I would parse the date strings into a Date Object

DateFormat format = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
Date date = format.parse("26 May 1960");

You could create an object that holds a Date object then make it comparable.

public class DateContainer implements Comparable<DateContainer > {
 private Date dateTime;

 public DateContainer (Date date){
  this.dateTime = date;
 }

 public Date getDateTime() {
  return dateTime;
 }

 public void setDateTime(Date datetime) {
  this.dateTime = datetime;
 }

 @Override
 public int compareTo(DateContainer o) {
   return getDateTime().compareTo(o.getDateTime());
 }
}

Then you could create a list of the Object above then use Collections to sort it

Collections.sort(myList);

Sorting, means that earlier values precede later ones e.g. 1/1/2000 will sort ahead of 1/1/2001. Sort dates in the format dd-mmm-yyyy Adds a new sorting option to dataTables called date-dd-mmm-yyyy. Also includes a type detection plug-in. Matches and sorts date strings in the format: dd/mmm/yyyy.

public static void main(String[] args) {
    SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy");
    String[] input = {"20 Oct 2052",
            "26 May 1960",
            "06 Jun 1933",
            "06 Jun 1933",
            "06 Jun 1933",
    };
    Map<Long, String> result = new TreeMap<>();
    Stream.of(input)
            .filter(s -> s.length() == 11)
            .forEach(s -> {
                try {
                    result.put(f.parse(s).getTime(), s);
                } catch (ParseException e) {
                    System.out.println("Wrong Format: " + s);
                }
            });
    System.out.println(result); // {-1154156400000=06 Jun 1933, -303033600000=26 May 1960, 2612970000000=20 Oct 2052}
}

Use SimpleDateFormat to get Date value and TreeMap to sorted elements in the map.

Hope that help you!!!!

I have a list of items which contain a date in the format dd mmm yyyy. code: <​xsl:variable name="FORMATTER" select="java:java.text. First get the list of dates from the user as a string. Create an array of objects for Date class and dynamically allocate memory. Create an object for SimpleDateFormat class with the format as “dd-MM-yyyy”. Parse the string dates to the date object. Sort the dates and print them. Program

This should do the trick

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy");

List<Date> dates = Arrays.stream(input).map(dateString -> {
try {
    return simpleDateFormat.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
}
    return null;
}).collect(Collectors.toList());
dates.sort(Comparator.naturalOrder());

dates.forEach(date -> System.out.println(simpleDateFormat.format(date)));

this is a 2 step process

  1. Convert to java.util.Date
  2. Sort the Date list and Print

Hope this helps!

Good luck!

Sort an array of string of dates in ascending order Note: Each date is of the form dd mmm yyyy where: Domain C++ program to sort the dates in a string array. Sort an array of string of dates in ascending order. Given an array of strings dates [], the task is to sort these dates in ascending order. Note: Each date is of the form dd mmm yyyy where: Domain of dd is [0-31]. Domain of mmm is [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]. And, yyyy is a four digit integer.

UPDATE: You can run performance tests I prepared based on various code samples from the answers to this question:  In order to format dates using SimpleDateFormat, we first needs to define a String date format e.g. "dd-MM-yyyy" will print dates in that format e.g. 01-11-2012.You can defined format based upon identifiers supported by SimpleDateFormat class. e.g. d means day of month, y means year and M means Month of year.

Best Java code snippets using java.util.Date.compareTo (Showing top 20 results out how to sort Date which is in string format in java? Collections.sort(​datestring, new Comparator<String>() { DateFormat f = new SimpleDateFormat("​dd/MM/yyyy hostname = getCookieHost(url); List<Cookie> cookies = new ArrayList<>();  Simple Date Format Demo: 27. Format date in Medium format: 28. Format date in Long format: 29. Format date in Full format: 30. Format date in Default format: 31. Formatting day of week using SimpleDateFormat: 32. Formatting day of week in EEEE format like Sunday, Monday etc. 33. Formatting day in d format like 1,2 etc: 34. Formatting day in dd

List; public class StringToDateUtil { private formatList.add(new SimpleDateFormat("dd MMM yyyy")); Note that you should create static object for a list of formats in your application rather than initializing it all the time. Next. How to convert Java Date into Specific TimeZone format. More specifically, the ISO 8601 date format. The ISO 8601 date format (hereafter just called ISO date format) looks like this: yyyy-MM-ddTHH:mm:ss:sssZ The ISO 8601 date format has the advantage, that if you sort date strings formatted using the ISO 8601 format, the strings will actually be sorted in date order.

Display Current Date in Java; SimpleDateFormat: Parse and Format Dates; Compare Dates Example The appropriate date format specified will be-hh:mm:​ss a dd-MMM-yyyy 59) Bubble Sort Program · 60) Insertion Sort Algorithm · 61) Selection Sorting · 62) Java Tools for Developers · 63) Groovy Script  [code]/** This is fully functional method to sort date*/ public static void main(String args) throws ParseException { ArrayList<Date> date=new ArrayList

Comments
  • I think PriorityQueue would be helpful in this case
  • Possible duplicate of How to sort Date which is in string format in java?
  • I wrote a new answer to the linked original question. Instead of LocalDateTime as in that answer use LocalDate (since your dates don’t have time of day), and use a DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH). That should give you the effective and efficient solution.
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.
  • So that's true, there are better implementations. But for the purpose of this question when they are just using Strings, anything solution is a better solution.
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.