That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014).

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

In your specific case of "January 2, 2010" as the input string:

  1. "January" is the full text month, so use the MMMM pattern for it
  2. "2" is the short day-of-month, so use the d pattern for it.
  3. "2010" is the 4-digit year, so use the yyyy pattern for it.
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

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Symbol Meaning Presentation Examples
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).


Pre-Java 8

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

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

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M/L Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
u Day number of week Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

Input string Pattern
2001.07.04 AD at 12:08:56 PDT yyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01 EEE, MMM d, ''yy
12:08 PM h:mm a
12 o'clock PM, Pacific Daylight Time hh 'o''clock' a, zzzz
0:08 PM, PDT K:mm a, z
02001.July.04 AD 12:08 PM yyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700 yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700 yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00 yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3 YYYY-'W'ww-u

An important note is that SimpleDateFormat is not thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scope.

Answer from BalusC on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java dates › convert string to date in java
Convert String to Date in Java | Baeldung
March 26, 2025 - By default, Java dates are in the ISO-8601 format, so if we have any string which represents a date and time in this format, then we can use the parse() API of these classes directly.
🌐
Stack Abuse
stackabuse.com › how-to-convert-a-string-to-date-in-java
How to Convert a String to Date in Java
September 28, 2018 - We've covered multiple ways to convert a simple String into Date and Date-Time classes in Java.
Discussions

How do I parse a String into java.sql.Date format?

Can't your SQL server accept strings for dates? The ones I've used can, so I just do something like:

String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd-MM-yyyy").parse(startDate));

... and pass sqlDate to the parametrized query. Like konrad mentioned, lowercase 'mm' is for minutes, and uppercase 'MM' is for month, so I think that's where your problem was.

More on reddit.com
🌐 r/java
5
1
April 4, 2012
Java string to date conversion - Stack Overflow
What is the best way to convert a String in the format 'January 2, 2010' to a Date in Java? Ultimately, I want to break out the month, the day, and the year as integers so that I can use Date dat... More on stackoverflow.com
🌐 stackoverflow.com
Convert String to java.util.Date - Stack Overflow
I am storing the dates in a SQLite database in this format: d-MMM-yyyy,HH:mm:ss aaa When I retrieve the date with that format I am get every thing fine except the hour. The hour is always 00. Her... More on stackoverflow.com
🌐 stackoverflow.com
Convert string into java.util.date
Please, don't use classes from `java.util.date` as these have been superseded by `java.time` package. If you need to parse date (without time), use `LocalDate.parse`. It comes in two flavours - one, which tries to parse your input with ISO_LOCAL_DATE formatter. The second one, which requires you to provide your own desired formatter as a second parameter. When you need both date and time, use `LocalDateTime.parse`. This one comes in two versions as well (one with implicit ISO_LOCAL_DATE_TIME formatter and another where you need to provide your own formatter). For more details, please check the official JDK docs for `java.time` package ( https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/package-summary.html ). The formatters are described in `java.time.format` package docs ( https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/format/package-summary.html ) More on reddit.com
🌐 r/javahelp
14
5
January 29, 2026
🌐
InfluxData
influxdata.com › home › how to convert string to date in java
How to Convert String to Date in Java | InfluxData
July 25, 2024 - Understanding how to perform these conversions efficiently and accurately is crucial for any Java developer, from beginner to intermediate. This article will guide you through converting String to Date in Java, explain why it’s necessary, and showcase various practical methods, complete with examples.
🌐
Sentry
sentry.io › sentry answers › java › how do i convert a string to a date in java?
How Do I Convert a String to a Date in Java? | Sentry
However, DateTimeFormatter requires Java version 8 or later. For older Java versions, you can use the SimpleDateFormat class from the java.text package. Here’s an example that demonstrates how to convert a String to a LocalDate using the DateTimeFormatter class:
🌐
Dariawan
dariawan.com › tutorials › java › convert-string-to-date-using-simpledateformat
Convert String to Date Using SimpleDateFormat | Dariawan
Use parse(...) method to convert from String to Date, use format(...) method to convert from Date into String. If you are already using Java 8 (and above), please look at DateTimeFormatter.
🌐
Reddit
reddit.com › r/java › how do i parse a string into java.sql.date format?
r/java on Reddit: How do I parse a String into java.sql.Date format?
April 4, 2012 -

Alright, so I've got a file that I am reading and importing dates into a database using SQL. In order to do this, I need to parse the date from the file into a java.sql.Date. Here is an example date.

01-16-2014

And I need it in a format like

2014-01-16

I tried this.

String startDate="01-06-2014";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());

But I got an error saying that I couldn't parse a java.util.Date to a java.sql.Date through a method invocation. Is there an easy way to do this?

Thanks in advance for your help!

🌐
Coderanch
coderanch.com › t › 746130 › java › turn-String-yyyyMM-java-util
How do I turn String yyyyMM into java.util.date? (Java in General forum at Coderanch)
October 5, 2021 - All Dates in Java are absolute, internally, and all are counted in milliseconds since Midnight January 1 1970 UTC. So you not only need to know year, month, and day, you need to know timezone and even that will actually give the value from Midnight of that timezone/day.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-date
Java Program to Convert String to Date - GeeksforGeeks
July 11, 2025 - Convert the String to Date using the Instant.parse() method. If converted successfully, then print the Date. If not converted successfully, then DateTimeParseException is thrown. ... // Java Program to Convert String to Date // Using Instant ...
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › convert_a_string_to_date_in_java
Write a Java program to convert a string to date
The pattern is "MMMM d, yyyy" which corresponds to the format of the ... Locale.ENGLISH specifies the locale of the formatter. LocalDate dt = LocalDate.parse(str_date, date_format);: This uses the ... import java.time.*; import java.util.*; import java.time.format.DateTimeFormatter; class String_ToDate { public static void main(String[] args) { String str_date = "July 8, 2014"; DateTimeFormatter date_format = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH); LocalDate dt = LocalDate.parse(str_date, date_format); System.out.println("String Date : "+str_date); System.out.println("Convert String to Date : "+dt); } }
Top answer
1 of 16
1853

That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014).

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

In your specific case of "January 2, 2010" as the input string:

  1. "January" is the full text month, so use the MMMM pattern for it
  2. "2" is the short day-of-month, so use the d pattern for it.
  3. "2010" is the 4-digit year, so use the yyyy pattern for it.
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

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Symbol Meaning Presentation Examples
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).


Pre-Java 8

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

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

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M/L Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
u Day number of week Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

Input string Pattern
2001.07.04 AD at 12:08:56 PDT yyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01 EEE, MMM d, ''yy
12:08 PM h:mm a
12 o'clock PM, Pacific Daylight Time hh 'o''clock' a, zzzz
0:08 PM, PDT K:mm a, z
02001.July.04 AD 12:08 PM yyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700 yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700 yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00 yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3 YYYY-'W'ww-u

An important note is that SimpleDateFormat is not thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scope.

2 of 16
85

Ah yes the Java Date discussion, again. To deal with date manipulation we use Date, Calendar, GregorianCalendar, and SimpleDateFormat. For example using your January date as input:

Calendar mydate = new GregorianCalendar();
String mystring = "January 2, 2010";
Date thedate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(mystring);
mydate.setTime(thedate);
//breakdown
System.out.println("mydate -> "+mydate);
System.out.println("year   -> "+mydate.get(Calendar.YEAR));
System.out.println("month  -> "+mydate.get(Calendar.MONTH));
System.out.println("dom    -> "+mydate.get(Calendar.DAY_OF_MONTH));
System.out.println("dow    -> "+mydate.get(Calendar.DAY_OF_WEEK));
System.out.println("hour   -> "+mydate.get(Calendar.HOUR));
System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
System.out.println("second -> "+mydate.get(Calendar.SECOND));
System.out.println("milli  -> "+mydate.get(Calendar.MILLISECOND));
System.out.println("ampm   -> "+mydate.get(Calendar.AM_PM));
System.out.println("hod    -> "+mydate.get(Calendar.HOUR_OF_DAY));

Then you can manipulate that with something like:

Calendar now = Calendar.getInstance();
mydate.set(Calendar.YEAR,2009);
mydate.set(Calendar.MONTH,Calendar.FEBRUARY);
mydate.set(Calendar.DAY_OF_MONTH,25);
mydate.set(Calendar.HOUR_OF_DAY,now.get(Calendar.HOUR_OF_DAY));
mydate.set(Calendar.MINUTE,now.get(Calendar.MINUTE));
mydate.set(Calendar.SECOND,now.get(Calendar.SECOND));
// or with one statement
//mydate.set(2009, Calendar.FEBRUARY, 25, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
System.out.println("mydate -> "+mydate);
System.out.println("year   -> "+mydate.get(Calendar.YEAR));
System.out.println("month  -> "+mydate.get(Calendar.MONTH));
System.out.println("dom    -> "+mydate.get(Calendar.DAY_OF_MONTH));
System.out.println("dow    -> "+mydate.get(Calendar.DAY_OF_WEEK));
System.out.println("hour   -> "+mydate.get(Calendar.HOUR));
System.out.println("minute -> "+mydate.get(Calendar.MINUTE));
System.out.println("second -> "+mydate.get(Calendar.SECOND));
System.out.println("milli  -> "+mydate.get(Calendar.MILLISECOND));
System.out.println("ampm   -> "+mydate.get(Calendar.AM_PM));
System.out.println("hod    -> "+mydate.get(Calendar.HOUR_OF_DAY));
🌐
Kodejava
kodejava.org › how-do-i-convert-string-to-date-object
How do I convert String into Date Object in Java? - Learn Java by Examples
To convert a string of date we can use the help from java.text.SimpleDateFormat that extends java.text.DateFormat abstract class. package org.kodejava.text; import java.text.DateFormat; import java.text.ParseException; import java.text.Simp...
🌐
BeginnersBook
beginnersbook.com › 2013 › 04 › java-string-to-date-conversion
How to Convert String to date in Java
Date: Wed Apr 02 00:00:00 IST 2014 Date in dd/MM/yyyy format is: 02/04/2014 Date: Wed Apr 02 23:37:50 IST 2014 Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50 Date: Wed Apr 02 00:00:00 IST 2014 Date in dd-MMM-yyyy format is: 02-Apr-2014 Date: Wed Apr 02 00:00:00 IST 2014 Date in MM dd, yyyy format is: 04 02, 2014 Date: Wed Apr 02 00:00:00 IST 2014 Date in E, MMM dd yyyy format is: Wed, Apr 02 2014 Date: Wed Apr 02 23:37:50 IST 2014 Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50 ... I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
🌐
GraphQL
graphql.org › learn › schema
Schemas and Types | GraphQL
2 weeks ago - The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable. In most GraphQL service implementations, there is also a way to specify custom Scalar types. For example, we could define a Date type:
🌐
Tutorial Gateway
tutorialgateway.org › java-program-to-convert-string-to-date
Java Program to Convert String to Date
April 2, 2025 - In this example, we used the LocalDate parse() method to convert string to date. First, we declared a string variable of str1 and assigned a random date. Next, we created the instance of the LocalDate class and used the parse() function associated ...
🌐
Stack Overflow
stackoverflow.com › questions › 64734797 › how-to-convert-a-inputstring-to-date-in-java
How to convert a input(string) to date in Java? - Stack Overflow
Instead use LocalDate and DateTimeFormatter, both from java.time, the modern Java date and time API. ... If you are getting an exception, please pate the full stack trace into your question, formatted as code for readability. And thanks for the further information. Please put that in your question too so we have everything in one place.
🌐
Oreate AI
oreateai.com › blog › bridging-the-gap-effortlessly-converting-strings-to-dates-in-java › 5925ea531ca4980c8521caadbe322f70
Bridging the Gap: Effortlessly Converting Strings to Dates in Java - Oreate AI Blog
1 month ago - And the reverse is just as straightforward. If you have a Date object and want to turn it into a specific string format, you use the same SimpleDateFormat object and call its format() method.
🌐
TechFliez Academy
techfliez.com › java-string-to-date
Java String to Date - TechFliez Academy
December 2, 2024 - 2. The parse() method of SimpleDateFormat converts the string into a Date object. 3. If the string doesn’t match the format, a ParseException will be thrown, so it’s important to catch that exception.
🌐
Udemy
blog.udemy.com › home › getting started with java string to date conversion
Getting Started with Java String to Date Conversion - Udemy Blog
May 13, 2014 - One of the classes is ‘java.text.SimpleDateFormat’, which is a concrete class (implement all methods of ‘DateFormat’ abstract class) for formatting and parsing dates in a locale-sensitive manner. It allows formatting of Date objects to a Java String, parsing Java String to Date object and normalization.