fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5. import java.time.*;
  6. import java.time.format.*;
  7.  
  8. class Ideone {
  9.  
  10. public static void main (String[] args) throws java.lang.Exception {
  11.  
  12. Person youngest;
  13.  
  14. // you could do string comparison if you know the timezones are consistent:
  15.  
  16. youngest = listOfPersons.stream()
  17. .max(Comparator.comparing(p -> p.dateOfBirth)).get();
  18.  
  19. System.out.println("youngest is: " + youngest.name + " " + youngest.dateOfBirth);
  20.  
  21. // or you could just parse the date, a safer option (choose ISO_INSTANT or ISO_DATE_TIME depending on needs):
  22.  
  23. youngest = listOfPersons.stream()
  24. .max(Comparator.comparing(p -> LocalDateTime.parse(p.dateOfBirth, DateTimeFormatter.ISO_DATE_TIME))).get();
  25.  
  26. System.out.println("youngest is: " + youngest.name + " " + youngest.dateOfBirth);
  27.  
  28. }
  29.  
  30.  
  31. //------------------------------------
  32. // types from OP:
  33.  
  34. static class Person{
  35. String name;
  36. String id;
  37. String dateOfBirth;
  38. int salary;
  39. }
  40.  
  41. static List <Person> listOfPersons;
  42.  
  43.  
  44. //------------------------------------
  45. // boring example data initialization:
  46.  
  47. // make an iso birthdate string given an age (for initializing data)
  48. static String fromAge (int years) {
  49. //return LocalDateTime.now().minusYears(years).toString();
  50. // add zone to emulate OP's input:
  51. return LocalDateTime.now().minusYears(years).toString() + 'Z';
  52. }
  53.  
  54. // make up some data
  55. static {
  56. listOfPersons = List.of(
  57. // fine for example, but i wouldn't init like this in production
  58. new Person() {{ name="helga"; dateOfBirth=fromAge(22); }},
  59. new Person() {{ name="mortimer"; dateOfBirth=fromAge(48); }},
  60. new Person() {{ name="gertrude"; dateOfBirth=fromAge(6); }},
  61. new Person() {{ name="jebediah"; dateOfBirth=fromAge(39); }}
  62. );
  63. listOfPersons.forEach(p -> System.out.println(p.name + ' ' + p.dateOfBirth));
  64. }
  65.  
  66.  
  67. }
Success #stdin #stdout 0.21s 55260KB
stdin
Standard input is empty
stdout
helga 2000-06-23T03:07:26.084850Z
mortimer 1974-06-23T03:07:26.126628Z
gertrude 2016-06-23T03:07:26.126894Z
jebediah 1983-06-23T03:07:26.127948Z
youngest is: gertrude 2016-06-23T03:07:26.126894Z
youngest is: gertrude 2016-06-23T03:07:26.126894Z