java.util.Date represents an instant in time, with no reference to a particular time zone or calendar system. It does hold both date and time though - it's basically a number of milliseconds since the Unix epoch.

Alternatively you can use java.util.Calendar which does know about both of those things.

Personally I would strongly recommend you use Joda Time which is a much richer date/time API. It allows you to express your data much more clearly, with types for "just dates", "just local times", "local date/time", "instant", "date/time with time zone" etc. Most of the types are also immutable, which is a huge benefit in terms of code clarity.

Answer from Jon Skeet on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
If you don't know what a package is, read our Java Packages Tutorial. To display the current date, import the java.time.LocalDate class, and use its now() method:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › › › › java › util › Date.html
Date (Java Platform SE 8 )
October 20, 2025 - A date (day of month) is represented by an integer from 1 to 31 in the usual manner. An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12. A minute is represented by an integer from 0 to 59 in the usual ...
🌐
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!
🌐
Tutorialspoint
tutorialspoint.com › java › java_date_time.htm
Java - Date and Time
Java provides the Date class available in java.util package, this class encapsulates the current date and time. The Date class supports two constructors as shown in the following table.
🌐
Medium
medium.com › @camillefauchier › choosing-the-right-date-type-in-java-for-your-context-4e66806b49f7
Choosing the Right Date Type in Java for Your Context | by Camille Fauchier | Medium
November 23, 2024 - Learn how to choose between LocalDateTime, ZonedDateTime, and OffsetDateTime in Java for precise, timezone-aware applications.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.date
Date Class (Java.Util) | Microsoft Learn
<li>A date (day of month) is represented by an integer from 1 to 31 in the usual manner. <li>An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › class-use › Date.html
Uses of Class java.util.Date (Java Platform SE 8 )
October 20, 2025 - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › date
Java | Date | Codecademy
June 21, 2023 - Date(long millis): Creates a Date object with the specified number of milliseconds since January 1, 1970, 00:00:00 GMT (the Unix epoch). ... SimpleDateFormat class (from java.text package): Allows formatting and parsing of dates using patterns.
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
🌐
Baeldung
baeldung.com › home › java › java dates › introduction to the java date/time api
Introduction to the Java Date/Time API | Baeldung
October 13, 2023 - Getter methods are also available to extract specific units similar to the date and time classes. Given the above instance of LocalDateTime, this code sample will return the month February: ... Java 8 provides ZonedDateTime when we need to deal with time-zone-specific date and time.
🌐
GeeksforGeeks
geeksforgeeks.org › java › date-class-java-examples
Date class in Java (With Examples) - GeeksforGeeks
January 2, 2019 - The class Date represents a specific instant in time, with millisecond precision. The Date class of java.util package implements Serializable, Cloneable and Comparable interface. It provides constructors and methods to deal with date and time ...
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java SE 10 & JDK 10 )
For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created.
🌐
InfluxData
influxdata.com › home › java date format: a detailed guide
Java Date Format: A Detailed Guide | InfluxData
July 12, 2024 - The Timestamp class in Java extends java.util.Date and represents a specific moment in time, including both date and time information, with nanosecond precision. Developers commonly use it when dealing with database operations or when they require ...
🌐
CodeGym
codegym.cc › java blog › java classes › java.util.date class
Java.util.Date Class
February 14, 2025 - Introduced in Java 8, it offers classes like LocalDate, LocalTime, LocalDateTime, and ZonedDateTime. These classes tackle all the headaches of the old APIs—time zone issues, thread safety, and more—while providing a clear, modern design.
🌐
Medium
medium.com › @shashane.ranasinghe › date-types-in-java-15a30b6070c0
Date types in Java. Util Date(java.util.Date) | by Shashane Ranasinghe | Medium
February 14, 2022 - Date types in Java Util Date(java.util.Date) The util date class represents a timestamp of a particular moment. it is a value in milliseconds which represents the number of milliseconds since the 1st …
Top answer
1 of 1
25

Each one of the Date classes are for specific purposes:

  • If you want to use your Date in an SQL/JDBC context, use the java.sql.Timestamp.

  • java.util.Date is the old Java API, it is not thread safe, you can difficultly handle time zoning, and on the top of all, it is poorly designed: one simple uniformity is that months start from 1 while days start from 0.

  • java.time.LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second, which you need exactly.

  • java.time.ZonedDateTime class stores all date and time fields, so you can use it to deal with values like: 27th January 1990 at 15:40.30.123123123 +02:00 in the Europe/Paris time-zone.

To do your task, the ZonedDateTime class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant (which models a single instantaneous point on the time-line). The difference between the two time-lines, represented by a ZoneOffset, is the offset from UTC/Greenwich.

To calculate duration and period: there is the java.time.Duration which is a time-based amount of time, such as '20.5 seconds', and java.time.Period, which is a date-based amount of time (like: 26 years, 2 months and 2 days).

To get max and min dates, you can use the Java 8 lambdas in something like:

Date maxDate = list.stream().map(yourInstance -> yourInstance.date).max(Date::compareTo).get();
Date minDate = list.stream().map(yourInstance -> yourInstance.date).min(Date::compareTo).get();
🌐
Medium
medium.com › codex › java-date-format-5a2515b07c2c
Java Date Format with Examples. java. util.Date | by Maneesha Nirman | CodeX | Medium
November 12, 2022 - Java Date Format with Examples java. util.Date This is used to represent date and time in Java. The class is used to keep coordinated universal time (UTC). The class represents a specific instant in …
🌐
Edureka
edureka.co › blog › date-format-in-java
Date Format In Java | Java Simple Date Format | Edureka
July 23, 2024 - To Convert the date to dd-mm-yyyy in Java, you use the Java—simpleDateFormat class. To write the dates in the dd-mm-yyyy format, you can use the Java—simpleDateFormat class.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Date.html
Date (Java Platform SE 7 )
A date (day of month) is represented by an integer from 1 to 31 in the usual manner. An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12. A minute is represented by an integer from 0 to 59 in the usual ...