Use LocalDateTime#parse() (or ZonedDateTime#parse() if the string happens to contain a time zone part) to parse a String in a certain pattern into a LocalDateTime.

String oldstring = "2011-01-18 00:00:00.0";
LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));

Then use LocalDateTime#format() (or ZonedDateTime#format()) to format a LocalDateTime into a String in a certain pattern.

String newstring = datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(newstring); // 2011-01-18

Or, when you're not on Java 8 yet, use SimpleDateFormat#parse() to parse a String in a certain pattern into a Date.

String oldstring = "2011-01-18 00:00:00.0";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

Then use SimpleDateFormat#format() to format a Date into a String in a certain pattern.

String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18

See also:

  • Java string to date conversion

Update: as per your failed attempt which you added to the question after this answer was posted; the patterns are case sensitive. Carefully read the java.text.SimpleDateFormat javadoc what the individual parts stands for. So stands for example M for months and m for minutes. Also, years exist of four digits yyyy, not five yyyyy. Look closer at the code snippets I posted here above.

Answer from BalusC on Stack Overflow
🌐
Jenkov
jenkov.com › tutorials › java-internationalization › simpledateformat.html
Java SimpleDateFormat
The pattern parameter passed to ... later in this text. The pattern is just a regular Java String. Once you have created a SimpleDateFormat instance you can format dates using its format() method. Here is an example:...
Top answer
1 of 16
552

Use LocalDateTime#parse() (or ZonedDateTime#parse() if the string happens to contain a time zone part) to parse a String in a certain pattern into a LocalDateTime.

String oldstring = "2011-01-18 00:00:00.0";
LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));

Then use LocalDateTime#format() (or ZonedDateTime#format()) to format a LocalDateTime into a String in a certain pattern.

String newstring = datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(newstring); // 2011-01-18

Or, when you're not on Java 8 yet, use SimpleDateFormat#parse() to parse a String in a certain pattern into a Date.

String oldstring = "2011-01-18 00:00:00.0";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

Then use SimpleDateFormat#format() to format a Date into a String in a certain pattern.

String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18

See also:

  • Java string to date conversion

Update: as per your failed attempt which you added to the question after this answer was posted; the patterns are case sensitive. Carefully read the java.text.SimpleDateFormat javadoc what the individual parts stands for. So stands for example M for months and m for minutes. Also, years exist of four digits yyyy, not five yyyyy. Look closer at the code snippets I posted here above.

2 of 16
177

Formatting are CASE-SENSITIVE so USE MM for month not mm (this is for minute) and yyyy For Reference you can use following cheatsheet.

G   Era designator  Text    AD
y   Year    Year    1996; 96
Y   Week year   Year    2009; 09
M   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 name in week    Text    Tuesday; Tue
u   Day number of week (1 = Monday, ..., 7 = Sunday)    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

Examples:

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

java - how to format date using SimpleDateFormat - Stack Overflow
My database has date of the format - 2012-02-16T00:00:00.000-0500 I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss ... What are you trying to do exactly? Why can't you use the SimpleDateFormat to parse the Date too. More importantly, why aren't you using the new Java 8 DateTime ... More on stackoverflow.com
🌐 stackoverflow.com
Printing out datetime in a specific format in Java? - Stack Overflow
Please read the question carefully. OP's question Printing out datetime in a specific format in Java? asks for a solution in Java, and not C#. Your code may work in C# but not in Java; please revise your answer or consider removing it as it currently doesn't answer OP's question. More on stackoverflow.com
🌐 stackoverflow.com
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 8, 2012
The Best Date Format
No option for YYMMDDhhmm stored as a 32-bit signed int? More on reddit.com
🌐 r/ProgrammerHumor
136
1518
January 1, 2022
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-simpledateformat-java-date-format
Master Java Date Formatting: SimpleDateFormat & DateFormat Guide | DigitalOcean
December 20, 2024 - These classes are part of the java.text ... formatting, making it highly customizable. For example, you can specify patterns like “dd-MM-yyyy” for dates or “HH:mm:ss” for times....
Top answer
1 of 2
5

Thanks to @Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet

Complete Solution:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SampleDate {
    public static void main(String[] args) throws ParseException {
        DateFormat parseFormat = new SimpleDateFormat(
                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
        String strDate = parseFormat.format(date);
        System.out.println(strDate);

        // if you get date of type 'java.sql.Date' directly from database cursor like
         //rs.getDate("created_date"), just pass it directly to format()

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm:ss");
        String stringDate = dateFormat.format(date);
        System.out.println(stringDate);

    }
}

/*
Output:

2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00

*/

you can also convert java.util.Date to java.sql.Date like this,

String dateString = "03-11-2012";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    java.util.Date date = dateFormat.parse(dateString);
    java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
2 of 2
3

If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:

DateFormat parseFormat = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");

Along with the rest of your code this writes:

16-Feb-2012 05:00:00

The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.

This answer assumes Java 7, or you would be using the new date & time API from Java 8

🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › text › SimpleDateFormat.html
SimpleDateFormat (Java SE 11 & JDK 11 )
January 20, 2026 - The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone. Date formats are not synchronized. It is recommended to create separate format instances for each thread.
🌐
GeeksforGeeks
geeksforgeeks.org › java › dateformat-format-method-in-java-with-examples
DateFormat format() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java Platform SE 8 )
October 20, 2025 - The withResolverFields(Tempora... month, day-of-month and day-of-year, then there are two approaches to resolve a date: (year + month + day-of-month) and (year + day-of-year)....
🌐
Baeldung
baeldung.com › home › java › java dates › a guide to simpledateformat
A Guide to SimpleDateFormat | Baeldung
January 8, 2024 - SimpleDateFormat supplies a vast array of different options when formatting dates. While the full list is available in the JavaDocs, let’s explore some of the more commonly used options: The output returned by the date component also depends heavily on the number of characters used within the String. For example, let’s take the month of June.
🌐
Medium
medium.com › @david_turner › java-date-formatting-and-utilization-e2a2e2534049
Java Date Formatting and Utilization | by David Turner | Medium
December 31, 2019 - For example, this was my toString() result on the following date string: Input: 2019-11-28T22:30:197 | Result: Thu Nov 28 22:30:19 CST 2019 · As you can see, my current running JVM captured this date object in CST — which could be problematic if my running solution is intended to be timezone agnostic. ... Java’s Calendar class provides a means of converting Date objects to other instances of times through a slew of methods.
🌐
InfluxData
influxdata.com › home › java date format: a detailed guide
Java Date Format: A Detailed Guide | InfluxData
July 12, 2024 - To convert a string into a date in Java, you can use SimpleDateFormat or DateTimeFormatter to parse the string according to a specified format pattern. The example below demonstrates how to parse a string representing a date (01/31/2024) into a date object using SimpleDateFormat.
🌐
Jenkov
jenkov.com › tutorials › java-date-time › parsing-formatting-dates.html
Parsing and Formatting Dates in Java
Here are a few pattern examples, with examples of how each pattern would format or expect to parse a date: yyyy-MM-dd (2009-12-31) dd-MM-YYYY (31-12-2009) yyyy-MM-dd HH:mm:ss (2009-12-31 23:59:59) HH:mm:ss.SSS (23:59.59.999) yyyy-MM-dd HH:mm:ss.SSS ...
🌐
Coderanch
coderanch.com › t › 412338 › java › date-format-Locale
Getting date format from Locale (Beginning Java forum at Coderanch)
October 19, 2008 - In case your DateFormat instance is not a SimpleDateFormat you could create a fallback solution by defining some kind of dummy date, use the steps of the first code block to retrive a formatted instance and use this one to get the numbering order and separating characters.
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › simple-date-format-java
Java SimpleDateFormat Class explained with examples
September 11, 2022 - In this example we are converting ... java.util.Date; public class Example { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd, MM, yyyy"); //converting date to string using format() method String dateString = ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 8 )
October 20, 2025 - The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone. Date formats are not synchronized. It is recommended to create separate format instances for each thread.
🌐
Edureka
edureka.co › blog › date-format-in-java
Date Format In Java | Java Simple Date Format | Edureka
July 23, 2024 - The DateFormat class in Java is used for formatting dates. A specified date can be formatted into the Data/Time string. For example, a date can be formatted into: mm/dd/yyyy.
🌐
BeginnersBook
beginnersbook.com › 2013 › 04 › java-date-format
Java Date Format examples
September 11, 2022 - There is another class SimpleDateFormat ... static void main(String args[]){ Date currentDate = new Date(); System.out.println("Current date is: "+currentDate); String dateShort = DateFormat.getDateInstance(DateFormat.SHORT).format(currentDate); System.out.println("Formatting the Date ...
🌐
Scaler
scaler.com › topics › date-format-in-java
Date Format in Java - Scaler Topics
April 20, 2022 - This method is used to convert this date and time into an appropriate format, i.e., mm/dd/yyyy. ... A simple example of how to format dates using the format() method is shown below.
🌐
Hostman
hostman.com › tutorials › java date format
Java Date Format | Guide by Hostman
March 31, 2025 - Explore Java's SimpleDateFormat for powerful date formatting and parsing, with custom patterns, locales, time zones, and best practices for robust applications.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland