fork download
  1. import java.util.Arrays;
  2. import java.util.Date;
  3. import java.util.TimeZone;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6.  
  7. class Datas {
  8. public static void main(String[] args) {
  9. String[] array = {"05/09/2016", "10/10/2012", "06/09/2016", "5/4/2012", "11/11/2016"};
  10. ordenarDatas2(array);
  11. System.out.println(Arrays.toString(array));
  12. }
  13.  
  14. public static String[] ordenarDatas2(String[] entrada) {
  15. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  16. sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  17. sdf.setLenient(false);
  18. Date[] dates = new Date[entrada.length];
  19. for (int i = 0; i < entrada.length; i++) {
  20. try {
  21. dates[i] = sdf.parse(entrada[i]);
  22. } catch (ParseException e) {
  23. throw new IllegalArgumentException(entrada[i], e);
  24. }
  25. }
  26. Arrays.sort(dates);
  27. for (int i = 0; i < entrada.length; i++) {
  28. entrada[i] = sdf.format(dates[i]);
  29. }
  30. return entrada;
  31. }
  32. }
Success #stdin #stdout 0.06s 711680KB
stdin
Standard input is empty
stdout
[05/04/2012, 10/10/2012, 05/09/2016, 06/09/2016, 11/11/2016]