Don't do it, that's the hard way. Moreover, those setter methods of
java.util.Date
are deprecated since Java 1.1 (1997). Just use SimpleDateFormat
(click the link to see all available format patterns).String string = "January 2, 2010";
DateFormat format =
new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
f you happen to be on Java 8 already, then use DateTimeFormatter
(also here, click the link to see all predefined formatters and available format patterns;
the tutorial is available here). This new API is inspired by JodaTime.
String string = "January 2, 2010";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
Comments
Post a Comment