Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]

后端 未结 4 888
死守一世寂寞
死守一世寂寞 2020-12-03 23:45

I thinking what is the best way in Java to parse the String with this format dd/MM/yyyy [to dd/MM/yyyy]. The string with the [] are optional and dd stand for the 2 digit pre

4条回答
  •  -上瘾入骨i
    2020-12-04 00:06

    tl;dr

    LocalDate.parse( 
        "22/01/2010" , 
        DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
    )
    

    …more…

    // String input is:
    // (a) long: "22/01/2010 to 23/01/2010". 
    // (b) short: "22/01/2010".
    // (c) null.
    
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
    
    if( input.length() == 24 ) {           // Ex: "22/01/2010 to 23/01/2010"
        List lds = new ArrayList<>( 2 );
        String[] inputs = input.split( " to " );
        for( String nthInput : inputs ) {
            LocalDate ld = LocalDate.parse( nthInput , f ) ;
            lds.add( ld );
        }
        … // Do what you want with `LocalDate` objects collection.
    
    } else if( input.length() == 10 ) {    // Ex: "22/01/2010"
        LocalDate ld = LocalDate.parse( input , f ) ;
        … // Do what you want with `LocalDate` object.
    
    } else if( null == input ) {
        … // Decide what you want to do for null input.
    
    } else {
        System.out.println( "Unexpected input: " + input ) ;
    }
    

    See this code run live at IdeOne.com.

    Using java.time

    The other Answers use troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

    As for handling multiple types of strings, look at the length of the string.

    if( input.length() == 10 ) { … }
    

    If long, split on the 4-character substring “ to ”.

    String[] inputs = "22/01/2010 to 23/01/2010".split( " to " );
    

    Parse the date string as a LocalDate.

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
    LocalDate ld = LocalDate.parse( "22/01/2010" , f );
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题