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 ;)

Answer from MadProgrammer on Stack Overflow
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);
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api
Date (Java Platform SE 8 )
3 days ago - JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
Discussions

display Java.util.Date in a specific format - Stack Overflow
Netiher an old-fashioned Date nor a modern LocalDatecan have a format in it. Link: Oracle tutorial: Date Time explaining how to use java.time. ... The question and the answers written at that time use java.util Date-Time API and their formatting API, SimpleDateFormat which was the right thing ... More on stackoverflow.com
🌐 stackoverflow.com
How do you deal with dates/timestamps?
I use java.time everywhere. It's like Joda, except better. Don't take my word for it, read what Joda's author and java.time's creator has to say. More on reddit.com
🌐 r/Clojure
7
14
May 3, 2017
how to get the current time as 'yyyy-mm-dd' in clojure?
(.format (new java.text.SimpleDateFormat "yyyy-MM-dd") (java.util.Date.)) More on reddit.com
🌐 r/Clojure
5
7
August 19, 2015
Formatting conversion error. java.util.UnknownFormatConversionException: Conversion = 'y'
You really shouldn't combine String concatenation with printf, because it can lead to hard-to-diagnose issues. Case in point, your problem. Look at the value of outputTaxPercent: outputTaxPercent = "12%"; Then you concatenate Strings: System.out.printf("Based on a marital status of "+outputStatus+" and a tax rate of "+outputTaxPercent+" your tax is $%.2f",taxUsed); As a result of the concatenation, this is the equivalent of: System.out.printf("Based on a marital status of married and a tax rate of 12% your tax is $%.2f",taxUsed); See the problem? Printf is trying to interpret 12% your as a format. I suggest doing this instead: System.out.printf("Based on a marital status of %s and a tax rate of %s your tax is $%.2f", outputStatus, outputTaxPercent, taxUsed); More on reddit.com
🌐 r/javahelp
4
4
February 10, 2019
🌐
Medium
medium.com › codex › java-date-format-5a2515b07c2c
Java Date Format with Examples. java. util.Date | by Maneesha Nirman | CodeX | Medium
November 12, 2022 - Let's have an idea of methods and constructors that can be found in util. Date class. But I am not going to discuss deprecated methods and constructors. ... This constructor initializes and allocates the Date object with the current date and time. It gets time from your local machine or server. import java.util.Date;public class A {public static void main(String[] args) {Date date = new Date(); System.out.println(date); } }
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-simpledateformat-java-date-format
Master Java Date Formatting: SimpleDateFormat & DateFormat Guide | DigitalOcean
December 20, 2024 - Master Java date and time formatting with SimpleDateFormat and DateFormat. Learn locale-based patterns and parsing techniques to enhance your java applications!
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › DateFormat.html
DateFormat (Java Platform SE 8 )
3 days ago - 8 ... DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date → text), parsing (text → date), and normalization.
🌐
How to do in Java
howtodoinjava.com › home › java date time › guide to java.util.date class
Guide to java.util.Date Class - HowToDoInJava
February 17, 2022 - Learn to create new date, get current date, parse date to string or format Date object using java.util.Date class.
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
The following example will remove ... " + myDateObj); DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formattedDate = myDateObj.format(myFormatObj); System.out.println("After ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java dates › convert java.util.date to string
Convert java.util.Date to String | Baeldung
January 8, 2024 - In this tutorial, we’ll show how we can convert Date objects to String objects in Java. To do so, we’ll work with the older java.util.Date type as well as with the new Date/Time API introduced in Java 8.
🌐
InfluxData
influxdata.com › home › java date format: a detailed guide
Java Date Format: A Detailed Guide | InfluxData
July 12, 2024 - Java provides several classes and methods for formatting and parsing dates. Let’s explore some of the essential ones: Despite being deprecated, developers still commonly use java.util.Date. It represents a specific instant in time but lacks a specific format and is primarily used for timestamp ...
🌐
Jenkov
jenkov.com › tutorials › java-internationalization › simpledateformat.html
Java SimpleDateFormat
The Java SimpleDateFormat class can parse and format dates according to custom date patterns. This Java SimpleDateFormat tutorial explains how to use the SimpleDateFormat class.
🌐
Edureka
edureka.co › blog › date-format-in-java
Date Format In Java | Java Simple Date Format | Edureka
July 23, 2024 - A specified date can be formatted into the Data/Time string. The format should be mm/dd/yyyy. According to the Java API, we can set the date using the constructor Date(year, month, day).
🌐
Tutorialspoint
tutorialspoint.com › java › java_date_time.htm
Java - Date and Time
It would be a bit silly if you had to supply the date multiple times to format each part. For that reason, a format string can indicate the index of the argument to be formatted. The index must immediately follow the % and it must be terminated by a $. import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", date); } }
🌐
Groovy
docs.groovy-lang.org › latest › html › groovy-jdk › java › util › Date.html
Date (Groovy JDK enhancements)
For example, if the system timezone is GMT, new Date(0).format('MM/dd/yy') would return the string "01/01/70". See documentation for SimpleDateFormat for format pattern use.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 8 )
3 days ago - Returns the beginning date of the 100-year period 2-digit years are interpreted as being within. ... Formats the given Date into a date/time string and appends the result to the given StringBuffer.
🌐
Javatpoint
javatpoint.com › java-simpledateformat
Java SimpleDateFormat
November 6, 2017 - Java SimpleDateFormat with concepts and examples of java SimpleDateFormat class, java date format program, java SimpleDateFormat methods, java SimpleDateFormat examples and more.
🌐
Hostman
hostman.com › tutorials › java date format
Java Date Format | Guide by Hostman
March 31, 2025 - Here’s an example demonstrating ... SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat verboseFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a z"); System.out.println("ISO ...
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Coderanch
coderanch.com › t › 676462 › java › java-util-Date
A little java.util.Date help [Solved] (Beginning Java forum at Coderanch)
February 23, 2017 - Yes, how can I change the date so it appears in this format? ... If I say to you: "Yesterday, a bit after 1 pm I went lunch". Can you tell me please, how many minutes were after 1 pm? Similar issues are with String you have. [edit] Probably you are asking something slightly different. Apologies if I misunderstood. ... The text passed in to the SimpleDateFormat defines the expected format of the String representation of the date you want to represent.
🌐
Medium
medium.com › @david_turner › java-date-formatting-and-utilization-e2a2e2534049
Java Date Formatting and Utilization | by David Turner | Medium
December 31, 2019 - Java’s Calendar class provides a means of converting Date objects to other instances of times through a slew of methods. Some of these include the obvious, like setting the YEAR, MONTH, DAY_OF_MONTH, etc to other utility based functions like adding or subtracting units of time.
🌐
Scaler
scaler.com › home › topics › date format in java
Date Format in Java - Scaler Topics
January 5, 2024 - Call the format() method of SimpleDateFormat without going through the Date object; this method returns a String representation of the specified date ... Parsing is the process of converting Strings into Java.util.Date instances.