日付でソート文字列[]

Cyber​​punk7711:

私は必死に例えば「2019年5月31日を」日付で配列をソートしてみてください。私は現在、当時の日付型に変換するとすることでこれをやろうとしています。また、これは動作します。私はちょうど] [日に] [文字列を変換することはできません。誰かがどのようにそれを行うには教えてもらえますか?または、必要であれば、私にそれをソートする簡単な方法を与えますか?残念ながら、私はオンライン、適切なものを発見していません。それはまた、私はあまりにも愚かだということができます。

私は日に手動で文字列を変換しようとしましたが、それは動作します。しかし、私は、String []型日に[]を変換するためにさまざまな方法を試してみました。私は、ループ...といくつかの他のものを試してみました...しかし、それは仕事をdoesntの..

public static void main(String[] args) throws ParseException {
    TagebuchIO io = new TagebuchIO();
    String[] stringio = io.getTagebuchliste();
    //He got the Dates with stringio
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyy");

    Date date = sdf.parse("10.05.2010");
    Date date2 = sdf.parse("09.02.2012");
    Date[] d = {date, date2};
    Arrays.sort(d);
    String ergbenis1 = sdf.format(d[0]);
    System.out.println(ergbenis1);
    ergbenis1 = sdf.format(d[1]);
    System.out.println(ergbenis1);
}
Shash678:

使用を検討してLocalDateというよりDate

import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.stream.Stream;

class Main {
  public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");

    String[] strDates = new String[] {"12.06.2019", "31.05.2019", "10.05.2010", "09.02.2012"};
    LocalDate[] dates = Stream.of(strDates).map(date -> LocalDate.parse(date, formatter))
                                           .toArray(LocalDate[]::new);

    System.out.println(String.format("Unsorted dates: %s", Arrays.toString(dates)));
    Arrays.sort(dates);
    System.out.println(String.format("Sorted dates: %s", Arrays.toString(dates)));

    System.out.println("Formatted Dates:");
    for (LocalDate d : dates) {
      System.out.println(formatter.format(d));
    }
  }
}

出力:

Unsorted dates: [2019-06-12, 2019-05-31, 2010-05-10, 2012-02-09]
Sorted dates: [2010-05-10, 2012-02-09, 2019-05-31, 2019-06-12]
Formatted Dates:
10.05.2010
09.02.2012
31.05.2019
12.06.2019

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=333317&siteId=1