fork download
  1. import java.text.ParsePosition;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.format.DateTimeParseException;
  5. import java.util.Optional;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
  10. String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";
  11.  
  12. // Processing english
  13. Optional<LocalDateTime> date = getDateTime(english);
  14. date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
  15. dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));
  16.  
  17. // Processing chinese
  18. date = getDateTime(chinese);
  19. date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
  20. dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));
  21. }
  22.  
  23. static Optional<LocalDateTime> getDateTime(String s) {
  24. // Assuming the date-time string is in the format, yyyy/MM/dd HH:mm:ss
  25. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/M/d H:m:s");
  26.  
  27. Optional<LocalDateTime> result = Optional.empty();
  28. for (int i = 0; i < s.length(); i++) {
  29. try {
  30. result = Optional.ofNullable(LocalDateTime.from(dtf.parse(s, new ParsePosition(i))));
  31. break;
  32. } catch (DateTimeParseException | IndexOutOfBoundsException e) {
  33. }
  34. }
  35. return result;
  36. }
  37. }
Success #stdin #stdout 0.09s 49556KB
stdin
Standard input is empty
stdout
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45