Created
June 1, 2018 23:26
-
-
Save kamatama41/eec9267f1ac8bc1435f4c6b8fd8bdff8 to your computer and use it in GitHub Desktop.
Please let me know if you have a smarter way 🙇
This file contains 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.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class DateParser { | |
public static void main(String[] args) { | |
System.out.println(parse("2018-06-01 12:00:00.111 JST")); | |
System.out.println(parse("2018-06-01 12:00:00 JST")); | |
} | |
private static Date parse(String date) { | |
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); | |
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); | |
try { | |
return formatter1.parse(date); | |
} catch (ParseException e) { | |
try { | |
return formatter2.parse(date); | |
} catch (ParseException e1) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment