Use java.text.DateFormat and java.text.SimpleDateFormat to do it.

DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy");
String dateAsString = "25/12/2010";
Date date = sourceFormat.parse(dateAsString);

UPDATE:

If you have two Dates hiding in that String, you'll have to break them into two parts. I think others have pointed out the "split" idea. I'd just break at whitespace and throw the "TO" away.

Don't worry about efficiency. Your app is likely to be riddled with inefficiencies much worse than this. Make it work correctly and refactor it only if profiling tells you that this snippet is the worst offender.

Answer from duffymo on Stack Overflow
Top answer
1 of 4
24

Use java.text.DateFormat and java.text.SimpleDateFormat to do it.

DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy");
String dateAsString = "25/12/2010";
Date date = sourceFormat.parse(dateAsString);

UPDATE:

If you have two Dates hiding in that String, you'll have to break them into two parts. I think others have pointed out the "split" idea. I'd just break at whitespace and throw the "TO" away.

Don't worry about efficiency. Your app is likely to be riddled with inefficiencies much worse than this. Make it work correctly and refactor it only if profiling tells you that this snippet is the worst offender.

2 of 4
1

tl;dr

LocalDate.parse( 
    "22/01/2010" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
)

…more…

// String input is:
// (a) long: "22/01/2010 to 23/01/2010". 
// (b) short: "22/01/2010".
// (c) null.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;

if( input.length() == 24 ) {           // Ex: "22/01/2010 to 23/01/2010"
    List<LocalDate> lds = new ArrayList<>( 2 );
    String[] inputs = input.split( " to " );
    for( String nthInput : inputs ) {
        LocalDate ld = LocalDate.parse( nthInput , f ) ;
        lds.add( ld );
    }
    … // Do what you want with `LocalDate` objects collection.

} else if( input.length() == 10 ) {    // Ex: "22/01/2010"
    LocalDate ld = LocalDate.parse( input , f ) ;
    … // Do what you want with `LocalDate` object.

} else if( null == input ) {
    … // Decide what you want to do for null input.

} else {
    System.out.println( "Unexpected input: " + input ) ;
}

See this code run live at IdeOne.com.

Using java.time

The other Answers use troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

As for handling multiple types of strings, look at the length of the string.

if( input.length() == 10 ) { … }

If long, split on the 4-character substring “ to ”.

String[] inputs = "22/01/2010 to 23/01/2010".split( " to " );

Parse the date string as a LocalDate.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate ld = LocalDate.parse( "22/01/2010" , f );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

🌐
Coderanch
coderanch.com › t › 554892 › databases › convert-date-MM-dd-yyyy
Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ? (JDBC and Relational Databases forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hi Guys, I'm developing an application where the user selects the date in (MM/dd/yyyy) format. Now, i convert the String (date recieved from user) to (yyyy-mm-dd) format by using the following java code , send it to valueOf() method of java.sql.Date class and then storing into DB.
🌐
Baeldung
baeldung.com › home › java › java dates › convert string to date in java
Convert String to Date in Java | Baeldung
March 26, 2025 - This library provides pretty much all the capabilities supported in the Java 8 Date Time project. ... <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.12.5</version> </dependency> Here’s a quick example working with the standard DateTime: DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"); String dateInString = "07/06/2013 10:11:59"; DateTime dateTime = DateTime.parse(dateInString, formatter);
🌐
Blogger
javarevisited.blogspot.com › 2011 › 09 › convert-date-to-string-simpledateformat.html
How to Convert Date to String in Java with Example
Anonymous said... SimpleDateFormat sdf=new SimpleDateFormat("DD-MM-YYYY"); java.util.Date up = sdf.parse(consupdated); System.out.println("date :"+up); but the output comes as wrong date why?
🌐
W3Docs
w3docs.com › java
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
The format() method is then used to format the Date object into a string using the MM-dd-yyyy format.
🌐
SAP Community
community.sap.com › t5 › technology-q-a › how-to-convert-string-yyyy-mm-dd-to-date-dd-mm-yyyy › qaq-p › 6227261
Solved: How to convert string yyyy-mm-dd to date dd/mm/yyy... - SAP Community
October 27, 2009 - String ds1 = "2007-06-30"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy"); String ds2 = sdf2.format(sdf1.parse(ds1)); System.out.println(ds2); //will be 30/06/2007 ... Migrate Java Mapping from SAP PO to SAP CPI/Integration Suite in Technology Blog Posts by Members 2024 Sep 12
🌐
Quora
quora.com › How-do-I-convert-dd-mm-yyyy-format-to-mm-dd-yyyy-in-Java
How to convert dd.mm.yyyy format to mm/dd/yyyy in Java - Quora
Answer (1 of 3): You can; * parse the date in one format and print it in the other. * or assume the input format is correct, sub-string the portions you need and create the string in the new format.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › format-date-with-simpledateformat-mm-dd-yy-in-java
Format date with SimpleDateFormat('MM/dd/yy') in Java
The format() method is called with different date patterns, including custom ones like "yyyy/MM/dd", "dd-M-yyyy hh:mm:ss", and "dd MMMM yyyy zzzz". After formatting, the date is printed in each format.
Top answer
1 of 6
94

Use this.

java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2012");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String format = formatter.format(date);
System.out.println(format);

you will get the output as

2012-12-01
2 of 6
5

Modern answer: Use LocalDate from java.time, the modern Java date and time API, and its toString method:

    LocalDate date = LocalDate.of(2012, Month.DECEMBER, 1); // get from somewhere
    String formattedDate = date.toString();
    System.out.println(formattedDate);

This prints

2012-12-01

Some will prefer the more explicit:

    String formattedDate = date.format(DateTimeFormatter.ISO_LOCAL_DATE);

ISO in the formatter name refers to the ISO 8601 standard. According to it a date is formatted the way you also want, as yyyy-mm-dd.

A date (whether we’re talking java.util.Date or java.time.LocalDate) doesn’t have a format in it. All it’s got is a toString method that produces some format, and you cannot change the toString method. Fortunately, LocalDate.toString produces exactly the format you asked for.

The Date class is long outdated, and the SimpleDateFormat class that you tried to use, is notoriously troublesome. I recommend you forget about those classes and use java.time instead. The modern API is so much nicer to work with.

Except: it happens that you get a Date from a legacy API that you cannot change or don’t want to change just now. The best thing you can do with it is convert it to java.time.Instant and do any further operations from there:

    Date oldfashoinedDate = // get from somewhere
    LocalDate date = oldfashoinedDate.toInstant()
            .atZone(ZoneId.of("Asia/Beirut"))
            .toLocalDate();

Please substitute your desired time zone if it didn’t happen to be Asia/Beirut. Then proceed as above.

Link: Oracle tutorial: Date Time, explaining how to use java.time.

🌐
Jenkov
jenkov.com › tutorials › java-internationalization › simpledateformat.html
Java SimpleDateFormat
String pattern = "EEEEE dd MMMMM yyyy HH:mm:ss.SSSZ"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale("da", "DK")); String date = simpleDateFormat.format(new Date()); System.out.println(date);
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › java-date-string-conversion
How to Convert Date to String in Java
import java.text.DateFormat; import ... MMM dd yyyy"); DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss"); try { //format() method Formats a Date into a date/time string....
🌐
TutorialsPoint
tutorialspoint.com › how-to-format-a-string-to-date-in-as-dd-mm-yyyy-using-java
How to format a string to date in as dd-MM-yyyy using java?
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.Period; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Scanner; public class CalculatingAge { public static Date StringToDate(String dob) throws ParseException{ //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); //Parsing the given String to Date object Date date = formatter.parse(dob); System.out.println("Date object value: "+date); return date; } pub
🌐
Quora
quora.com › How-do-you-get-a-yyyy-mm-dd-format-in-Java
How to get a yyyy-mm-dd format in Java - Quora
For example: LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd"); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter); All letters 'A' to 'Z' and ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-date-to-string
Java Program to Convert Date to String - GeeksforGeeks
July 23, 2025 - // Java Program to convert date ... used SimpleDateFormat date_format1 = new SimpleDateFormat("MM/dd/yyyy"); String date_str = date_format1.format(date); System.out.println("MM/dd/yyyy : " + date_str); // another date format ...
🌐
Blogger
javarevisited.blogspot.com › 2011 › 09 › step-by-step-guide-to-convert-string-to.html
How to convert String to Date in Java - SimpleDateFormat Example
Date Conversion from String to sql Date in Java: String startDate="01-02-2013"; SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date date = sdf1.parse(startDate) java.sql.Date sqlStartDate = new Date(date.getTime());
Top answer
1 of 8
184

Date is a container for the number of milliseconds since the Unix epoch ( 00:00:00 UTC on 1 January 1970).

It has no concept of format.

Java 8+

LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(ldt));
System.out.println(ldt);

Outputs...

05-11-2018
2018-05-11
2018-05-11T17:24:42.980

Java 7-

You should be making use of the ThreeTen Backport

Original Answer

For example...

Date myDate = new Date();
System.out.println(myDate);
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(myDate));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(myDate));
System.out.println(myDate);

Outputs...

Wed Aug 28 16:20:39 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 16:20:39 EST 2013

None of the formatting has changed the underlying Date value. This is the purpose of the DateFormatters

Updated with additional example

Just in case the first example didn't make sense...

This example uses two formatters to format the same date. I then use these same formatters to parse the String values back to Dates. The resulting parse does not alter the way Date reports it's value.

Date#toString is just a dump of it's contents. You can't change this, but you can format the Date object any way you like

try {
    Date myDate = new Date();
    System.out.println(myDate);

    SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
    SimpleDateFormat dmyFormat = new SimpleDateFormat("yyyy-MM-dd");

    // Format the date to Strings
    String mdy = mdyFormat.format(myDate);
    String dmy = dmyFormat.format(myDate);

    // Results...
    System.out.println(mdy);
    System.out.println(dmy);
    // Parse the Strings back to dates
    // Note, the formats don't "stick" with the Date value
    System.out.println(mdyFormat.parse(mdy));
    System.out.println(dmyFormat.parse(dmy));
} catch (ParseException exp) {
    exp.printStackTrace();
}

Which outputs...

Wed Aug 28 16:24:54 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 00:00:00 EST 2013
Wed Aug 28 00:00:00 EST 2013

Also, be careful of the format patterns. Take a closer look at SimpleDateFormat to make sure you're not using the wrong patterns ;)

2 of 8
37
SimpleDateFormat("MM-dd-yyyy");

instead of

SimpleDateFormat("mm-dd-yyyy");

because MM points Month, mm points minutes

SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sm.format(myDate);
🌐
Squash
squash.io › how-to-change-date-format-in-a-java-string
How to Change the Date Format in a Java String
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy"); 4. Use the format() method of the SimpleDateFormat class to format the Date object into a string with the desired output format.