Java - How to check if a value is of type Date

后端 未结 6 584
耶瑟儿~
耶瑟儿~ 2021-01-29 09:22

I have a project requirement. There are values in a .txt as -

 02/01/2017 00:00:00

Now I need to have some rules to check if this value in the

6条回答
  •  借酒劲吻你
    2021-01-29 10:18

    If you just want valid days, use non-lenient parsing:

    String dateString = "02/28/2017 00:00:00";
    
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    df.setLenient(false);
    Date date;
    try {
        date = df.parse(dateString);
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    This will throw an exception for non-existing days like the 31st of February

提交回复
热议问题