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.Duration;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.time.ZoneId;
  11. import java.time.ZoneOffset;
  12. import java.time.ZonedDateTime;
  13. import java.time.format.DateTimeFormatter;
  14. import java.time.format.DateTimeParseException;
  15. import java.util.Locale;
  16.  
  17. /* Name of the class has to be "Main" only if the class is public. */
  18. class Ideone
  19. {
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22.  
  23. // Run with out time zone (assumes UTC).
  24. Duration dShort = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" );
  25. System.out.println ( "dShort: " + dShort );
  26.  
  27. Duration dLong = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" );
  28. System.out.println ( "dLong: " + dLong );
  29.  
  30. // Run with specified time zone.
  31. ZoneId z = ZoneId.of ( "America/Montreal" );
  32. Duration dShortZoned = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" , z );
  33. System.out.println ( "dShortZoned: " + dShortZoned );
  34.  
  35. Duration dLongZoned = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" , z );
  36. System.out.println ( "dLongZoned: " + dLongZoned );
  37.  
  38. }
  39. }
  40.  
  41.  
  42.  
  43. /**
  44.   *
  45.   * @author Basil Bourque
  46.   */
  47. class DurationProcessor {
  48.  
  49. static final int SHORT = 30;
  50. static final int LONG = 41;
  51.  
  52. static final DateTimeFormatter FORMATTER_LOCALDATETIME = DateTimeFormatter.ofPattern ( "uuuu-MM-dd hh:mm a" );
  53. static final DateTimeFormatter FORMATTER_LOCALTIME = DateTimeFormatter.ofPattern ( "hh:mm a" );
  54.  
  55. static public Duration process ( String input ) {
  56. return DurationProcessor.process ( input , ZoneOffset.UTC );
  57. }
  58.  
  59. static public Duration process ( String input , ZoneId zoneId ) {
  60. Duration d = Duration.ZERO; // Or maybe null. To be generated by the bottom of this code.
  61.  
  62. if ( null == input ) {
  63. // …
  64. System.out.println ( "ERROR - Passed null argument." );
  65. return d;
  66. }
  67. if ( input.length () == 0 ) {
  68. // …
  69. System.out.println ( "ERROR - Passed empty string as argument." );
  70. return d;
  71. }
  72.  
  73. String inputModified = input.toUpperCase ( Locale.ENGLISH ); // Change `am` `pm` to `AM` `PM` for parsing.
  74.  
  75. String[] parts = inputModified.split ( " - " );
  76. String inputStart = parts[ 0 ]; // A date-time sting.
  77. String inputStop = parts[ 1 ]; // Either a date-time string or a time-only string (assume the same date).
  78.  
  79. ZonedDateTime start = null; // To be generated in this block of code.
  80. try {
  81. LocalDateTime ldt = LocalDateTime.parse ( inputStart , DurationProcessor.FORMATTER_LOCALDATETIME );
  82. start = ldt.atZone ( zoneId );
  83. } catch ( DateTimeParseException e ) {
  84. // …
  85. System.out.println ( "ERROR - The start failed to parse. inputStart: " + inputStart );
  86. return d;
  87. }
  88.  
  89. ZonedDateTime stop = null; // To be generated in this block of code.
  90. switch ( input.length () ) {
  91. case DurationProcessor.SHORT: // Example: "2008-01-01 01:00 pm - 01:56 pm"
  92. try {
  93. LocalTime stopTime = LocalTime.parse ( inputStop , DurationProcessor.FORMATTER_LOCALTIME );
  94. stop = ZonedDateTime.of ( start.toLocalDate () , stopTime , zoneId );
  95. } catch ( DateTimeParseException e ) {
  96. // …
  97. System.out.println ( "ERROR - The stop time failed to parse." );
  98. return d;
  99. }
  100. break;
  101. case DurationProcessor.LONG: // "2008-01-01 8:30 pm - 2008-01-02 09:30 am"
  102. try {
  103. LocalDateTime ldt = LocalDateTime.parse ( inputStop , DurationProcessor.FORMATTER_LOCALDATETIME );
  104. stop = ldt.atZone ( zoneId );
  105. } catch ( DateTimeParseException e ) {
  106. // …
  107. System.out.println ( "ERROR - The stop date-time failed to parse." );
  108. return d;
  109. }
  110. break;
  111. default:
  112. // …
  113. System.out.println ( "ERROR - Input string is of unexpected length: " + input.length () );
  114. break;
  115. }
  116.  
  117. d = Duration.between ( start , stop );
  118. return d;
  119. }
  120.  
  121. public static void main ( String[] args ) {
  122. // Run with out time zone (assumes UTC).
  123. Duration dShort = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" );
  124. System.out.println ( "dShort: " + dShort );
  125.  
  126. Duration dLong = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" );
  127. System.out.println ( "dLong: " + dLong );
  128.  
  129. // Run with specified time zone.
  130. ZoneId z = ZoneId.of ( "America/Montreal" );
  131. Duration dShortZoned = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" , z );
  132. System.out.println ( "dShortZoned: " + dShortZoned );
  133.  
  134. Duration dLongZoned = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" , z );
  135. System.out.println ( "dLongZoned: " + dLongZoned );
  136.  
  137. }
  138. }
Success #stdin #stdout 0.13s 712192KB
stdin
Standard input is empty
stdout
dShort: PT56M
dLong: PT13H
dShortZoned: PT56M
dLongZoned: PT13H