Java SimpleDateFormat Class
SimpleDateFormat is a class in Java used for formatting and parsing dates and times, belonging to the java.text package. It allows you to convert a date object (Date) into a string in a specific format, or parse a formatted date string back into a date object.
Basic Usage
1. Creating a SimpleDateFormat Object
To use SimpleDateFormatFirst, create an instance and specify a date format pattern:
Example
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a SimpleDateFormat object and specify the format
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
)
;
package), which solves many problems of the Date class:
Date
now
=
new
Date
(
)
;
// Format a date into a string
String
formattedDate
=
sdf.
format
(
now
)
;
System
.
out
.
println
(
Current time:
formattedDate
)
;
}
}
The output is:
当前时间: 2025-05-01 11:35:07
2. Common Format Pattern Symbols
The following are the commonly used format pattern symbols:
| sign before positive numbers | Meaning | Example |
|---|---|---|
| yyyy | Four-digit year | 2023 |
| MM | Two-digit month | 01-12 |
| dd | Two-digit day | 01-31 |
| HH | Hour in 24-hour format | 00-23 |
| mm | Minute | 00-59 |
| ss | Second | 00-59 |
| E | Day of week | Mon, Tue, etc. |
| a | AM/PM marker | AM or PM |
Date Formatting
data to the child component via Date object formatted into a string:
Example
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd EEEE a hh:mm:ss"
)
;
Date
now
=
new
Date
(
)
;
System
.
out
.
println
(
sdf.
format
(
now
)
)
;
// Output example: 2023-11-15 Wednesday PM 03:45:22
Date Parsing
Parse a formatted date string into a Date object:
Example
try
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
)
;
String
dateString
=
"2023-11-15"
;
Date
date
=
sdf.
parse
(
dateString
)
;
System
.
out
.
println
(
date
)
;
// Output: Wed Nov 15 00:00:00 CST 2023
}
catch
(
ParseException
e
)
{
e.
printStackTrace
(
)
;
}
Note: Parsing may throw a ParseException, so exception handling is required.
Thread Safety Issues
SimpleDateFormat is not thread-safe. If the same SimpleDateFormat instance is shared in a multithreaded environment, problems may occur. Solutions:
- Create a new instance each time (lower performance)
- Use
ThreadLocalKeep one instance per thread - Use Java 8's
DateTimeFormatter(recommended)
Example
// Solution using ThreadLocal
private
static
final
ThreadLocal
<
SimpleDateFormat
dateFormat
=
ThreadLocal.
withInitial
(
(
)
->
new
SimpleDateFormat
(
"yyyy-MM-dd"
)
)
;
// Usage
String
formattedDate
=
dateFormat.
get
(
)
.
format
(
new
Date
(
)
)
;
Java 8 Alternative
In Java 8 and higher versions, it is recommended to use the java.time classes from the package:
Example
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a formatter
DateTimeFormatter formatter
=
DateTimeFormatter.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
)
;
// Get the current time and format it
LocalDateTime now
=
LocalDateTime.
now
(
)
;
String
formattedDate
=
now.
format
(
formatter
)
;
System
.
out
.
println
(
Current time:
formattedDate
)
;
// Parse a string into a date-time
LocalDateTime parsedDate
=
LocalDateTime.
parse
(
"2023-11-15 14:30:00"
, formatter
)
;
System
.
out
.
println
(
"Parsed time: "
parsedDate
)
;
}
}
DateTimeFormatter is thread-safe, has better performance, and is recommended for new projects.
Summary
SimpleDateFormatUsed for date formatting and parsing- Specify the date format via a pattern string
- is not thread-safe; be careful when using it in multithreaded environments
- For Java 8 and above, it is recommended to use
DateTimeFormatter
By mastering SimpleDateFormat, allowing you to flexibly handle various date-time format conversions in Java programs.
The following are the commonly used methods of the java.text.SimpleDateFormat class:
Constructor
| Method | Description |
|---|---|
SimpleDateFormat() |
Constructs an object using the default pattern and the date format symbols for the default locale |
SimpleDateFormat(String pattern) |
Constructs an object using the given pattern and the date format symbols for the default locale |
SimpleDateFormat(String pattern, Locale locale) |
Constructs an object using the given pattern and the date format symbols for the given locale |
Common Format Pattern Symbols
| sign before positive numbers | Meaning | Example |
|---|---|---|
y |
Year | yyyy → 2023 |
M |
Month | MM → 07, MMM → Jul, MMMM → July |
d |
Day of month | dd → 15 |
H |
Hour (0-23) | HH → 14 |
h |
Hour (1-12) | hh → 02 |
m |
Minute | mm → 30 |
s |
Second | ss → 45 |
S |
Millisecond | SSS → 789 |
E |
Day of week | EEE → Mon, EEEE → Monday |
a |
AM/PM marker | a → PM |
z |
Timezone | z → CST, zzzz → China Standard Time |
Core Methods
| Method | Description |
|---|---|
String format(Date date) |
Formats a Date into a date/time string |
Date parse(String source) |
Parses text from a string to produce a Date object |
void applyPattern(String pattern) |
Applies a new date-time pattern |
String toPattern() |
Returns the current date-time pattern string |
void setTimeZone(TimeZone zone) |
Set Time Zone |
TimeZone getTimeZone() |
Gets the time zone |
void setLenient(boolean lenient) |
Sets whether parsing is lenient (default true) |
boolean isLenient() |
Checks whether parsing is lenient |