Last active
January 9, 2019 17:12
-
-
Save nherbaut/253a09dbb0d52b4a841528a323bbb1c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.temporal.TemporalAccessor | |
| import java.text.ParsePosition; | |
| import java.time.format.DateTimeFormatter; | |
| TemporalAccessor myParseDate(String str,Collection<DateTimeFormatter> formatters) throws java.time.format.DateTimeParseException{ | |
| for(DateTimeFormatter formatter : formatters){ | |
| int parsePosition=0; | |
| while(parsePosition<str.length()){ | |
| try{ | |
| TemporalAccessor ta = formatter.parse(str, new ParsePosition(parsePosition)); | |
| return ta; | |
| } | |
| catch(java.time.format.DateTimeParseException e){ | |
| parsePosition++; | |
| } | |
| } | |
| } | |
| throw new java.time.format.DateTimeParseException("failed to parse date entirely",str,0); | |
| } | |
| Collection<DateTimeFormatter> formatters=new ArrayList<>(); | |
| formatters.add(DateTimeFormatter.ofPattern("MMM dd, y",java.util.Locale.ENGLISH)); | |
| formatters.add(DateTimeFormatter.ofPattern("dd/MM/y",java.util.Locale.FRENCH)); | |
| for(String testString : new String[]{"toto May 15, 2015 aarg","je suis allé à l'école le 12/10/2001"}){ | |
| TemporalAccessor ta = myParseDate(testString,formatters); | |
| System.out.println(ta); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment