Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java Date Formatting (DateFormat and SimpleDateFormat) Tutorial
Formatting and parsing dates are common tasks in Java. The java.text.DateFormat
class provides methods for formatting and parsing dates in a locale-sensitive manner. However, DateFormat
is an abstract class, so you'll typically use the java.text.SimpleDateFormat
class, which extends DateFormat
. In this tutorial, we'll cover how to format and parse dates using the SimpleDateFormat
class.
The SimpleDateFormat
class allows you to define custom patterns for formatting and parsing dates. To create a SimpleDateFormat
object, pass a pattern string to its constructor. Some common pattern symbols include:
y
: Year (e.g., yyyy
for 4-digit year, yy
for 2-digit year)M
: Month (e.g., MM
for 2-digit month, MMM
for abbreviated month, MMMM
for full month name)d
: Day of the month (e.g., dd
for 2-digit day)E
: Day of the week (e.g., E
for abbreviated day name, EEEE
for full day name)H
: Hour in 24-hour format (e.g., HH
for 2-digit hour)h
: Hour in 12-hour format (e.g., hh
for 2-digit hour)m
: Minute (e.g., mm
for 2-digit minute)s
: Second (e.g., ss
for 2-digit second)a
: AM/PM markerTo format a java.util.Date
object, use the format()
method of SimpleDateFormat
.
Example:
import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { Date currentDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(currentDate); System.out.println("Formatted date: " + formattedDate); } }
In the example above, we created a SimpleDateFormat
object with a custom pattern and formatted the current date.
To parse a date string into a java.util.Date
object, use the parse()
method of SimpleDateFormat
. Note that the parse()
method throws a java.text.ParseException
, so you need to handle it properly.
Example:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { String dateString = "2022-05-04 12:30:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date parsedDate = sdf.parse(dateString); System.out.println("Parsed date: " + parsedDate); } catch (ParseException e) { System.out.println("Error parsing date: " + e.getMessage()); } } }
In the example above, we created a SimpleDateFormat
object with a custom pattern and parsed a date string into a Date
object.
In summary, the SimpleDateFormat
class provides a flexible way to format and parse dates in Java. By specifying custom patterns, you can easily convert between Date
objects and string representations of dates.
Formatting dates in Java using SimpleDateFormat
SimpleDateFormat
is a class in Java that allows you to format dates according to a specified pattern.
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); Date currentDate = new Date(); String formattedDate = sdf.format(currentDate); System.out.println("Formatted Date: " + formattedDate); } }
Custom date format patterns in Java SimpleDateFormat
You can create custom date format patterns using symbols like dd
for day, MM
for month, yyyy
for year, etc.
SimpleDateFormat customFormat = new SimpleDateFormat("MMMM dd, yyyy");
Parsing dates with SimpleDateFormat in Java
SimpleDateFormat
can also be used to parse strings into Date
objects.
String dateString = "2023-01-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date parsedDate = sdf.parse(dateString);
Java Date to String conversion using SimpleDateFormat
Converting a Date
object to a formatted string is commonly done for displaying dates.
Date currentDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(currentDate);
Changing date format in Java with DateFormat
The DateFormat
class is an abstract class for date/time formatting. SimpleDateFormat
is a concrete implementation of DateFormat
.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); Date currentDate = new Date(); String formattedDate = df.format(currentDate); System.out.println("Formatted Date: " + formattedDate); } }
Handling time zones in Java date formatting
You can set the time zone for SimpleDateFormat
to handle dates in different time zones.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Converting String to Date in Java using SimpleDateFormat
Parsing a string into a Date
object is useful when dealing with user input or external data.
String dateString = "2023-01-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date parsedDate = sdf.parse(dateString);