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

后端 未结 6 582
耶瑟儿~
耶瑟儿~ 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:26

    public static boolean isValidDate(String inDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        try {
          dateFormat.parse(inDate.trim());
        } catch (ParseException pe) {
          return false;
        }
        return true;
      }
    
    public static void main(String[] args) {
     String file = "/path/to/your/file.txt";
    
    try {
    
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line;
        while ((line = br.readLine()) != null) {
            // do something with line
        }
        br.close();
       isValidDate(line));
       //do what you want :)
      }
    

提交回复
热议问题