I have dates in text format of the form dd-mmm-yy or d-mmm-y, where months are abbreviations in letters (for example, 4-Nov-09 or 12-Dec-05, etc...) I would like to parse it to produce a java.util.Date object.
Could this be achieved by leveraging the java.text.DateFormat class? Or is there another easy way of doing this?
You could try this library dateparser.
It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly.
With it, you don't have to prepare any appropriate patterns:
The
date
will beMon Dec 12 00:00:00 CST 2005
Is it cool?
You can do it with
java.text.SimpleDateFormat
. Click the link, you'll see all patterns.For 1-2 digit days you can use the
d
pattern. For 3-character month abbreviations you can use theMMM
pattern. For 2 digit years you can use theyy
pattern.So the following should do:
java.time
This is the modern answer. The
DateFormat
class mentioned in the question and itsSimpleDateFormat
subclass used in most of the old answers are notoriously troublesome and long outdated. Don’t use any of those. Don’t useDate
either, it’s poorly designed and long outdated too.Since you have got a two-digit year (09 and 05 in your examples), you need to decide on a century. In the first round I am assuming that the year is in the range 2000 through 2099.
First we need a formatter:
DateTimeFormatter
is thread safe, so we’re happy with one instance for all to use. @shoover is correct that we should provide a locale. Use the formatter like this:Output is:
Validate
For validation, in particular of the correct interpretation of the two-digit year, I recommend that you add a range check. For example, to validate that the parsed date is not more than 10 years into the past or future:
For many purposes you will know in advance that the date is either in the past or in the future and can narrow down the valid interval even further than in my example.
Other centuries
If the years can go outside the 20xx range, we control the interpretation of the year through a
DateTimeFormatterBuilder
and itsappendValueReduced
method. Refer to one of the related answers in the list of links below.Links
SimpleDateFormat is the class normally used for this. Note that this class is not thread-safe (somewhat counter-intuitively) and if you're using this in a threaded context, then investigating Joda and its DateTimeFormatter is worthwhile.
Check out SimpleDateFormat
examples
Whatever you do, please use a Locale. That way, you'll still get reasonable results when the input comes in with a month name in French.