Home > OS >  How do I get the maximum date (in YYYYMM format) from an array of dates in java?
How do I get the maximum date (in YYYYMM format) from an array of dates in java?

Time:01-11

I want to find out the largest date from an array of dates in yyyyMM format. For example, suppose my arraylist of dates are:

["202210", "202211", "202212", "202301"]

then the correct value should be 202301

I tried using the SimpleDateFormat class and then parse the dates and then find the max date, like this:

List<String> dates = Arrays.asList("202210", "202211" ,"202212", "202301");  
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM", Locale.getDefault());
List<Date> temp = new ArrayList<>();
try {
    for (String date: dates) {
        temp.add(sdf.parse(date));
    }
} catch (ParseException e) {
    e.printStackTrace();
}
System.out.println(Collections.max(temp)); //shows Sun Jan 01 00:00:00 GMT 2023

How do I convert the Sun Jan 01 00:00:00 GMT 2023 to 202301?

CodePudding user response:

Assuming you want to display the max date in the same original format as the list, you need not convert to a bona fide date, assuming the date strings are always in the format yyyyMM. In this case, the strings will sort properly as dates, and we can simply use Collections#max directly:

List<String> dates=Arrays.asList("202210","202211","202212","202301"); 
System.out.println(Collections.max(dates));  // 202301

CodePudding user response:

Since the array is of String type, you can simply find largest string in chronological order and use that. Your current logic to find the max will still work or you can use some Collection to do that for you.

It will give you correct result since your format is yyyyMM which means a later year will always be larger than string value of a smaller year.

E.g. "202210" will always be less than "202211" or "202301"

Treating them as String will also save you computational time to parse them into Date object.

CodePudding user response:

You can — of course — sort the Strings and get the last value, but if you want to use something comparable that represents a month in a year, use a java.time.YearMonth, e.g.

public static void main(String[] args) {
    // example values
    String[] monthsWithYears = { "202210","202211","202212","202301" };
    // formatter capable of the String format
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMM");
    // map to list of YearMonth and find the maximum
    YearMonth maxYearMonth = Arrays.stream(monthsWithYears)
                                   .map(s -> YearMonth.parse(s, dtf))
                                   .max(YearMonth::compareTo)
                                   .get();
    // print it(s toString() method implicitly)
    System.out.println("Max year month is "   maxYearMonth);
}

Output:

Max year month is 2023-01

CodePudding user response:

Just replace the last line System.out.println(Collections.max(temp)); to System.out.println(sdf.format(Collections.max(temp)));

CodePudding user response:

An alternative solution using only a simple for-loop to determine the largest value with string comparison:

List<String> dates=Arrays.asList("202210","202211","202212","202301");
String max = "000000";
for (int i = 0; i < dates.size(); i  ) {
    if (dates.get(i).compareTo(max) > 0) {
        max = dates.get(i);
    }
}

System.out.println("Max date is: "   max); //Max date is: 202301
  • Related