用户在Java8中输入错误的日期格式时如何给出错误消息

时间:2019-04-04 19:52:19

标签: java datetime-format localdate

如果用户使用错误的格式输入,我想给出一条错误消息。正确的格式为“ yyyy-MM-dd HH:mm:ss”。我怎样才能把它作为条件?

例如 如果(yyyy <0) {sout(“请输入正确的年份”)}

这是我用来询问用户并对其进行格式化的代码

Scanner keyboard = new Scanner(System.in);
        String hour = "00:00:00";
        System.out.println("Please enter Date : ");
        String time = keyboard.next()+" "+ hour;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);

2 个答案:

答案 0 :(得分:1)

您可以使用正则表达式比较:

while input doesn't match the regex pattern
     print "Please enter date in the correct format: yyyy-MM-dd HH:mm:ss"
continue with the rest of the code

RegEx模式可以是:

  

\ d {4}-[01] \ d- [0-3] \ d [0-2] \ d:[0-5] \ d:[0-5] \ d(?:。\ d +)?Z?

您可以使用this网站来创建和测试RegEx模式

答案 1 :(得分:0)

如果用户仅输入日期(例如2019-03-31),则您的程序也不必担心一天中的时间。此外,您的格式是ISO 8601,该格式是LocalDate和其他java.time类解析(也可以打印)的默认格式。因此,您不需要显式的格式化程序。

我了解您想要进行范围检查,这当然是可取的。另外,如果用户输入完全不同的格式,则解析将抛出DateTimeParseException,您应该捕获该 LocalDate minAcceptedDate = LocalDate.of(0, Month.JANUARY, 1); LocalDate maxAcceptedDate = LocalDate.of(4000, Month.DECEMBER, 31); Scanner keyboard = new Scanner(System.in); System.out.println("Please enter Date : "); while (true) { String time = keyboard.next(); try { LocalDate dateTime = LocalDate.parse(time); if (dateTime.isBefore(minAcceptedDate) || dateTime.isAfter(maxAcceptedDate)) { System.out.println("Please enter a date in the range " + minAcceptedDate + " through " + maxAcceptedDate); } else { // OK break; } } catch (DateTimeParseException dtpe) { System.out.println("Please enter a date in format yyyy-mm-dd"); } } 并采取相应的措施。例如:

Please enter Date : 
Yesterday
Please enter a date in format yyyy-mm-dd
-001-12-30
Please enter a date in format yyyy-mm-dd
5000-12-12
Please enter a date in the range 0000-01-01 through 4000-12-31
2016-09-22

示例会话:

array([[[0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        ...,
        [1.24829336, 0.96461449, 3.35142857, 0.74675   , 0.776075  ],
        [1.248303  , 0.96427925, 0.        , 1.317225  , 1.317225  ],
        [1.24831488, 0.96409169, 2.74857143, 1.353775  , 1.377825  ]],

       [[0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        ...,
        [1.24969672, 0.96336315, 0.        , 1.319725  , 1.319725  ],
        [1.24968077, 0.96331624, 0.        , 1.33535   , 1.33535   ],
        [1.24969598, 0.96330252, 5.01714286, 1.3508    , 1.3947    ]],

       [[0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        ...,
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        [1.25715364, 0.95520672, 2.57714286, 1.04565   , 1.0682    ],
        [1.25291274, 0.96879701, 7.76      , 1.311875  , 1.379775  ]],

       ...,

       [[0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ],
        ...,
        [1.24791079, 0.96561021, 4.44      , 0.7199    , 0.75875   ],
        [1.25265263, 0.96117379, 2.09714286, 0.7636    , 0.78195   ],
        [1.25868651, 0.96001674, 3.01142857, 1.35235   , 1.3787   ]]])