0

I have a csv file and in it there is a string representation of a date and time

2014-05-11T09:00:01.772401

I am trying to verify it follows this format. My thought was to use a regex to match a pattern like 4 numbers followed by a - 2 numbers followed by a - and so on and so forth but i'm not familiar enough with regexes to build it.

Is validation of a string matching a specific pattern best accomplished using regexes or should I use something else. If regex is the way can you please provide the regex and explain what Char's represent in the validation process.

Also any links that could better explain regex pattern matching would be greatly appreciated.

1

1 Answer 1

2

You may use SimpleDateFormat to determine if the string representation is valid.

From your string

2014-05-11T09:00:01.772401

It seems the pattern would be:yyyy-MM-dd'T'hh:mm:ss.SSSSSS

Example usage:

public static void main(String[] args) {
    String t = "2014-05-11T09:00:01.772401";
    final String pattern = "yyyy-MM-dd'T'hh:mm:ss.SSSSSS";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {
        sdf.parse(t);
        System.out.println("Valid");
    } catch(java.text.ParseException e) {
        System.out.println("Invalid format");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Didnt even think about using SimpleDateFormat to check validity thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.