Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse date and time flexibly (Level 8) #62

Closed
luo-git opened this issue Aug 24, 2020 · 2 comments
Closed

How to parse date and time flexibly (Level 8) #62

luo-git opened this issue Aug 24, 2020 · 2 comments

Comments

@luo-git
Copy link

luo-git commented Aug 24, 2020

I can't think of any other way except checking time string against multiple regex, and my regex is pretty bad.

For example here's one:

Pattern p = Pattern.compile("(?<year>\\d{4})[\\s\\-.](?<month>\\d{1,2})[\\s\\-.](?<day>\\d{1,2})"
        + "[\\s\\-.T](?<hour>\\d{1,2})[\\s\\-.:]?(?<minute>\\d{2})");
Matcher m = pattern.matcher(timeString);
m.matches();

Matcher.group(groupName) can then be used to access the individual capture groups.
If the pattern cannot be matched then I just store the raw string as time.

It will be great if anyone can share a elegant approach for parsing date and time.

Thanks in advance!

@qwoprocks
Copy link

I think the method described in the link below is pretty simple and elegant
https://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat

@luo-git
Copy link
Author

luo-git commented Aug 24, 2020

This is gold!

I think the method described in the link below is pretty simple and elegant
https://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat

@luo-git luo-git closed this as completed Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants