fork(1) 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. import java.time.format.*;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. Ideone app = new Ideone() ;
  17. app.process( "22/01/2010 to 23/01/2010" );
  18. app.process( "22/01/2010" );
  19. app.process( "pink bunnies" );
  20.  
  21. }
  22.  
  23. private void process( String input ) {
  24.  
  25. // String input is:
  26. // (a) long: "22/01/2010 to 23/01/2010".
  27. // (b) short: "22/01/2010".
  28. // (c) null.
  29.  
  30. DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
  31.  
  32. if( input.length() == 24 ) { // Ex: "22/01/2010 to 23/01/2010"
  33. List<LocalDate> lds = new ArrayList<>( 2 );
  34. String[] inputs = input.split( " to " );
  35. for( String nthInput : inputs ) {
  36. LocalDate ld = LocalDate.parse( nthInput , f ) ;
  37. lds.add( ld );
  38. }
  39. System.out.println( "Result: " + lds ); // Do what you want with `LocalDate` objects collection.
  40.  
  41. } else if( input.length() == 10 ) { // Ex: "22/01/2010"
  42. LocalDate ld = LocalDate.parse( input , f ) ;
  43. System.out.println( "Result: " + ld ); // Do what you want with `LocalDate` object.
  44.  
  45. } else if( input == null ) {
  46. System.out.println( "xxx" ); // Decide what you want to do for null input.
  47.  
  48. } else {
  49. System.out.println( "Unexpected input: " + input ) ;
  50. }
  51.  
  52. }
  53. }
Success #stdin #stdout 0.13s 4386816KB
stdin
Standard input is empty
stdout
Result: [2010-01-22, 2010-01-23]
Result: 2010-01-22
Unexpected input: pink bunnies