fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8. import java.time.temporal.*;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16. List<String> inputs = new ArrayList<> ();
  17. inputs.add ( "2016-01-23" );
  18. inputs.add ( "2016-01-25" );
  19. inputs.add ( "2016-02-22" ); // End of longest period between dates.
  20. inputs.add ( "2016-02-25" );
  21. inputs.add ( "2016-02-28" );
  22.  
  23. LocalDate longestStart = null;
  24. LocalDate longestStop = null;
  25. LocalDate previousDate = null;
  26. long longestInDays = 0;
  27.  
  28. for ( String input : inputs ) {
  29. LocalDate currentDate = LocalDate.parse ( input );
  30. if ( null == previousDate ) { // First loop.
  31. previousDate = currentDate;
  32. continue; // Skip the rest of this first loop.
  33. }
  34. long currentDays = ChronoUnit.DAYS.between ( previousDate , currentDate );
  35. if ( currentDays > longestInDays ) {
  36. // If the current loop exceeds previous longest, remember this one as longest.
  37. longestInDays = currentDays;
  38. longestStart = previousDate;
  39. longestStop = currentDate;
  40. }
  41. // Prepare for next loop.
  42. previousDate = currentDate;
  43. }
  44.  
  45. System.out.println ( "Longest period has days: " + longestInDays + " = " + longestStart + "/" + longestStop );
  46.  
  47. }
  48. }
Success #stdin #stdout 0.1s 711680KB
stdin
Standard input is empty
stdout
Longest period has days: 28 = 2016-01-25/2016-02-22