Skip to content

Instantly share code, notes, and snippets.

@biniama
Last active November 19, 2019 15:31
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 biniama/06ea50e10af2f2c3d603e3626c034349 to your computer and use it in GitHub Desktop.
Save biniama/06ea50e10af2f2c3d603e3626c034349 to your computer and use it in GitHub Desktop.
Find date format given the date object
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Source: https://stackoverflow.com/questions/7579227/how-to-get-the-given-date-string-formatpattern-in-java
*/
public class DateFormatFinder {
private static final String[] formats = {
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd HH:mm:ss",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
"MM/dd/yyyy'T'HH:mm:ss.SSSZ",
"MM/dd/yyyy'T'HH:mm:ss.SSS",
"MM/dd/yyyy'T'HH:mm:ssZ",
"MM/dd/yyyy'T'HH:mm:ss",
"yyyy:MM:dd HH:mm:ss",
"yyyyMMdd",
};
public static void main(String[] args) {
String yyyyMMdd = "20110917";
parse(yyyyMMdd);
}
public static void parse(String d) {
if (d != null) {
for (String parse : formats) {
SimpleDateFormat sdf = new SimpleDateFormat(parse);
try {
sdf.parse(d);
System.out.println("Printing the value of " + parse);
} catch (ParseException e) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment