Create a Java Date from a String and vise versa

后端 未结 3 1815
借酒劲吻你
借酒劲吻你 2021-01-28 23:52

I have a date in Integer format(YYYYMMDD). And a start_time as a String (HH:mm 24 hour system)

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 00:08

    Strange Data Types For Date-Time

    Using:

    • An int to represent the digits of a calendar date
    • A string to represent time-of-day digits
    • A double to represent a duration of fractional hours

    …are all unusual approaches. Probably not the wisest choices in handling date-time values.

    Avoid java.util.Date/Calendar

    Know that the bundled classes java.util.Date and .Calendar are notoriously troublesome and should be avoided. Use either Joda-Time or the new java.time.* package (Tutorial) in Java 8. And get familiar with the handy ISO 8601 standard.

    Time Zone

    Your question and example ignore the crucial issue of time zone. Handling date-time data without time zone is like handling text files without knowing their character encoding. Not good.

    Use proper time zone names to create time zone object. Avoid the non-standard 3-letter codes.

    Joda-Time

    In Joda-Time, a DateTime object is similar to a java.util.Date object but actually knows its own assigned time zone.

    Joda-Time offers three classes for representing spans of time: Period, Duration, and Interval.

    The Interval class uses the "Half-Open" approach, where the beginning is inclusive and the ending is exclusive. This approach works well for handling spans of time and comparisons. Look for the handy contains, abuts, overlap, and gap methods.

    int dateInput = 20140214;
    String start_timeInput = "14:30";
    double durationInput = 50.30;
    
    // Clean up these inputs.
    String datePortion = Integer.toString( dateInput );
    String input = datePortion + " " + start_timeInput; 
    DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "yyyyMMdd HH:mm");
    
    // Specify the time zone this date-time represents.
    DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); // Or, DateTimeZone.UTC
    DateTime dateTime = formatterInput.withZone( timeZone ).parseDateTime( input );
    
    // Convert fractional hours to milliseconds, then milliseconds to a Duration object.
    long millis = ( 60L * 60L * (long)(1000L * durationInput) ); // 1 hour = 60 minutes * 60 seconds * 1000 milliseconds.
    Duration duration = new Duration( millis );
    
    Interval interval = new Interval( dateTime, duration );
    
    DateTimeFormatter formatterOutput = DateTimeFormat.forStyle( "MM" ).withLocale( Locale.FRANCE );
    String description = "De " + formatterOutput.print( interval.getStart() ) + " à " + formatterOutput.print( interval.getEnd() );
    

    Dump to console…

    System.out.println( "input: " + input );
    System.out.println( "dateTime: " + dateTime );
    System.out.println( "duration: " + duration ); // Format: PnYnMnDTnHnMnS (from ISO 8601)
    System.out.println( "interval: " + interval ); // Format: / (from ISO 8601)
    System.out.println( "description: " + description );   
    

    When run…

    input: 20140214 14:30
    dateTime: 2014-02-14T14:30:00.000+01:00
    duration: PT181080S
    interval: 2014-02-14T14:30:00.000+01:00/2014-02-16T16:48:00.000+01:00
    description: De 14 févr. 2014 14:30:00 à 16 févr. 2014 16:48:00
    

提交回复
热议问题