Convert a Date to a String using DateFormat#format method:

String pattern = "MM/dd/yyyy HH:mm:ss";

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date according to the chosen pattern
DateFormat df = new SimpleDateFormat(pattern);

// Get the today date using Calendar object.
Date today = Calendar.getInstance().getTime();        
// Using DateFormat format method we can create a string 
// representation of a date with the defined format.
String todayAsString = df.format(today);

// Print the result!
System.out.println("Today is: " + todayAsString);

From http://www.kodejava.org/examples/86.html

Answer from Ali Ben Messaoud 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 - Learn several methods for converting Date objects to String objects 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 2, 2012
The best way to extract Date from String (Java)
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
5
0
November 3, 2023
Parse String with relative time and convert to Date: is there a library?
For the calculation you can use the buildin java classes like Duration, LocalDateTime and so on. No idea about the parsing, but if you have the calculation, parsing is relatively simple. More on reddit.com
🌐 r/javahelp
5
14
January 27, 2019
[Java] help converting user inputted string of a date to a different format
Look into using a DateFormat to parse the text to a Date object, and use a second DateFormat to format that date in your second format. You can add two weeks to it by adding the appropriate number of milliseconds to date's "time." You'll probably want to use SimpleDateFormat . More on reddit.com
🌐 r/learnprogramming
2
1
September 12, 2013
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));
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-convert-date-to-string-in-java
How to convert Date to String in Java - GeeksforGeeks
Create a DateTimeFormatter object to format the LocalDate object as a string. Use the format() method of the DateTimeFormatter object to convert the LocalDate object to a string. Print the result.
Published   July 15, 2025
🌐
Baeldung
baeldung.com › home › java › java dates › convert java.util.date to string
Convert java.util.Date to String | Baeldung
January 8, 2024 - private static final String EXPECTED_STRING_DATE = "Aug 1, 2018 12:00 PM"; private static final String DATE_FORMAT = "MMM d, yyyy HH:mm a"; Now we need actual Date object that we’d like to convert. We’ll use a Calendar instance to create it:
🌐
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:
🌐
Blogger
javarevisited.blogspot.com › 2011 › 09 › convert-date-to-string-simpledateformat.html
How to Convert Date to String in Java with Example
It’s very important for any java developer to be it senior or junior to get familiarize himself with Date, Time, and Calendar API. SimpleDateFormat is an excellent utility for converting String to Date and then Date to String but you just need to be a little careful with format and thread safety. ... Jirka Pinkas said... Nice! I'd like to add that you can find well arranged table with all permitted letters used in format patterns like yyyy.MM.dd in Javadoc API: http://download.oracle.com/javase/7/docs/api/index.html?java/text/SimpleDateFormat.html And also you can choose not to use class SimpleDateFormat.
Find elsewhere
🌐
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.
🌐
Java67
java67.com › 2018 › 01 › how-to-change-date-format-of-string-in-java8.html
How to Format Date to String in Java 8 [Example Tutorial] | Java67
You can use the DateTimeFormatter class in JDK 8 to change the date format of String in Java 8. The steps are exactly the same but instead of using SimpleDateFormat and Date class, we'll use the DateTimeFormatter and LocalDateTime class.
🌐
Scaler
scaler.com › home › topics › how does one convert date object to a string?
How Does One Convert Date Object to a String? - Scaler Topics
October 5, 2021 - This article will teach about DateFormat classes like SimpleDateFormat and the java.time.format.DateTimeFormatter from the new Date and Time API in JDK 8. In the function String_to_date, which as the name suggests helps to convert date objects ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-date-to-string
Java Program to Convert Date to String - GeeksforGeeks
July 23, 2025 - Given a date, we need to convert the date to a given format of the string. ... The Calendar class is used to provide functions for carrying out interconversion between any instant in time and a set of calendar fields. The Calendar class has an in-built method getInstance() which is used to fetch the current date and time. It returns the date which is stored in a Date type variable. DateFormat class is an in-built class in Java ...
🌐
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 ...
🌐
Edureka
edureka.co › blog › convert-string-to-date-in-java
How To Convert String To Date In Java | Java Programming | Edureka
July 5, 2024 - The best way to convert is String to Date ... If you need the month as a text in three letters, we need to define 3 ‘M’ which is taken as the month value. Then the value of the month is interpreted as text like Oct, Dec, Jun etc. ... Here is the code to express the String value in the Date format. Package com.test.test import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample1 { public static void main(String[] argv) { SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); String dateInString = "12-Dec-1998"; try { Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); }catch (ParseException e) { e.printStackTrace(); } } }
🌐
How to do in Java
howtodoinjava.com › home › java date time › format a date to string in java
Format a Date to String in Java with Examples - HowToDoInJava
August 21, 2025 - This class is thread-safe and immutable ... instance to string, we first need to create DateTimeFormatter instance with desired output pattern and then use its format() method to format the date....
🌐
Javatpoint
javatpoint.com › java-string-to-date
Java String to Date - javatpoint
July 30, 2022 - Java Convert String to Date example and examples of string to int, int to string, string to date, date to string, string to long, long to string, string to char, char to string, int to long, long to int etc.
🌐
Coderanch
coderanch.com › t › 692753 › java › Convert-Date-String
Convert Date to a String (Java in General forum at Coderanch)
April 10, 2018 - (And then you're converting the Date back to another String. But that part seems to be okay... right?) ... Didn't try it but I think it should work. JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... ...wouldn't you want double M and double d? In case I use the MM and dd there is an exception: java.time.format.DateTimeParseException: Text '4/6/2018' could not be parsed at index 0 But, this will work if the input date string also has the matching double M's and double d's: "04/06/2018".
🌐
Delft Stack
delftstack.com › home › howto › java › how to convert date to string in java
How to Convert Date to String in Java | Delft Stack
February 2, 2024 - If you are using the Apache library then use format() method of DateFormateUtils class. It returns a string after converting java.util.Date to string in Java.
🌐
LabEx
labex.io › tutorials › java-how-to-format-a-date-object-into-a-specific-string-format-in-java-414030
How to format a Date object into a specific string format in Java | LabEx
November 6, 2017 - Overall, the Date class provides a convenient way to work with dates and times in your Java applications. In Java, you can format a Date object as a string using the SimpleDateFormat class, which is part of the java.text package.
🌐
Coderanch
coderanch.com › t › 753349 › java › Convert-java-util-Date-string
Convert java.util.Date to string (JSP forum at Coderanch)
August 22, 2014 - When I put plain date for example 2022-06-23 and 2022-07-01, then the result is 8. ... Serpher Onwave wrote:... My object is to determine number of days between days. You would likely do well to look at the java.time.LocalDateTime class for this. It has far more features for this kind of thing built in, rather than having to mess about for yourself.
🌐
Educative
educative.io › answers › how-to-convert-string-to-date-in-java-8
How to convert string to date in Java 8
The parse method of the LocalDate class is used to perform this conversion. The syntax for the parse method of the LocalDate class in Java 8 is: The code snippet below illustrates how the LocalDate class and the​ DateTimeFormatter class perform the string to date conversion in Java 8: