Java 8 Java.Time Package: Parsing Any String to Date [Code Snippets]

DZone 's Guide to

Java 8 Java.Time Package: Parsing Any String to Date [Code Snippets]

Learn more about parsing Java Strings into date formats.

· Java Zone ·
Free Resource

In one of my projects, I received a requirement that stated that while parsing a text file, Strings denoting a date or a timestamp are expected to be in many different formats that are not known in advance, yet all of them represent a valid date or timestamp needed to be parsed properly. So, the solution I came up with is this: To have a set of formats stored in the property file, and when a String needs to be parsed, the formats are read from a file and attempts to parse the String are made sequentially with each format until it is parsed successfully, or until we run out of formats. The advantages of this solution are that if you discover a valid String that was not parsed successfully, all you will need to do is to add a new format to your properties file and no re-compilation and re-deployment is needed. Also, this way, you can set your priorities: Say if the US date format is preferable to the European one, just place US formats first and only after the European ones. Also, in Java 8, the format Strings allow for the optional format sections denoted by '[]'. So, several formats actually may be combined into a single one with optional sections. For example, instead of:




You can just write:


So, below is my set of formats that I found to cover a wide range of valid date formats. This set, of course, does not cover ALL possible formats. For instance, it does not cover an option that date includes milliseconds. But I think it is a good set to start with if you have ever had such a requirement. So here it is:

M[M]['/']['-']['.']d[d]['/']['-']['.']yyyy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

M[M]['/']['-']['.']d[d]['/']['-']['.']yyyy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']M[M]['/']['-']['.']yyyy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']M[M]['/']['-']['.']yyyy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

yyyy['/']['-']['.']M[M]['/']['-']['.']d[d][' ']['T'][' ']H[H]:mm:ss[.SSS][' ']['('][VV][X][x][Z][z][O][')']

yyyy['/']['-']['.']M[M]['/']['-']['.']d[d][' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

M[M]['/']['-']['.']yyyy['/']['-']['.']d[d][' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

M[M]['/']['-']['.']yyyy['/']['-']['.']d[d][' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']MMM['/']['-']['.']yyyy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']MMM['/']['-']['.']yy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']MMM['/']['-']['.']yyyy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

d[d]['/']['-']['.']MMM['/']['-']['.']yy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

MMM['/']['-']['.']d[d]['/']['-']['.']yyyy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

MMM['/']['-']['.']d[d]['/']['-']['.']yy[' ']['T'][' ']H[H]:mm:ss[' ']['('][VV][X][x][Z][z][O][')']

MMM['/']['-']['.']d[d]['/']['-']['.']yyyy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

MMM['/']['-']['.']d[d]['/']['-']['.']yy[' ']['T'][' ']h[h]:mm:ss[' ']a[' ']['('][VV][X][x][Z][z][O][')']

Happy formatting!

date and time ,java ,string ,format ,tutorial ,java 8 ,java.time ,parse

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}