Skip to content

Instantly share code, notes, and snippets.

@kamatama41
Created June 1, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamatama41/eec9267f1ac8bc1435f4c6b8fd8bdff8 to your computer and use it in GitHub Desktop.
Save kamatama41/eec9267f1ac8bc1435f4c6b8fd8bdff8 to your computer and use it in GitHub Desktop.
Please let me know if you have a smarter way 🙇
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