Abdul D.
—You don’t know how to convert a String
to a Date
in Java.
The recommended way to convert a String
to a Date
in Java is to use the DateTimeFormatter
class from java.time
. The DateTimeFormatter
class is thread-safe, making it a better choice for multithreaded environments.
However, DateTimeFormatter
requires Java version 8 or later. For older Java versions, you can use the SimpleDateFormat
class from the java.text
package.
DateTimeFormatter
Here’s an example that demonstrates how to convert a String
to a LocalDate
using the DateTimeFormatter
class:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String dateString = "2024-10-09"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date = LocalDate.parse(dateString, formatter); System.out.println(date); } }
This generates the following output:
2024-10-09
SimpleDateFormat
Here’s an example that demonstrates how to convert a String
to a Date
using the SimpleDateFormat
class:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class Main { public static void main(String[] args) { String dateString = "2024-10-09"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = formatter.parse(dateString); System.out.println("Converted Date: " + date); } catch (ParseException e) { System.out.println("Invalid date format"); } } }
This generates the following output:
Converted Date: Mon Oct 09 00:00:00 UTC 2024
In this example, SimpleDateFormat
is used to define the format of the input string (yyyy-MM-dd
). The parse()
method attempts to convert the string into a Date
object. If the input string doesn’t match the expected format, a ParseException
is thrown.
Note: The SimpleDateFormat
class is not thread-safe, so you should avoid sharing instances across multiple threads.
The SimpleDateFormat
and DateTimeFormatter
classes allow you to define custom date formats, such as:
dd/MM/yyyy
for dates like 09/10/2024
MM-dd-yyyy HH:mm:ss
for dates with times, like 10-09-2024 14:00:00
Ensure the format you provide matches the input string to avoid ParseException
errors.
The table below provides an overview of the available format pattern symbols for the DateTimeFormatter
class:
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; |
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.