Java SimpleDateFormat is a class in the Java API that provides a way to format and parse dates and times using a specified pattern. It allows developers to easily create customized date and time formats for output or to parse strings into dates and times.
The class is part of the java.text package and has been available in Java since version 1.1. SimpleDateFormat uses a pattern string to specify the format of the date or time to be parsed or formatted. The pattern string contains special characters that are used to represent different parts of the date and time, such as the year, month, day, hour, minute, and second.
Here is an example of using SimpleDateFormat to format a date:
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.format(now); System.out.println(formattedDate); } }
In this example, we create a new SimpleDateFormat object with a pattern of “yyyy-MM-dd HH:mm:ss”. This specifies the format of the date to be year-month-day hour:minute:second. We then use the format() method to format the current date and time (stored in the now variable) as a string using this pattern. Finally, we print the formatted date to the console. The output will look something like this: “2023-05-07 10:30:15”.
Constructors of the Class SimpleDateFormat:
The SimpleDateFormat class provides several constructors for creating instances with different options for date and time formatting. Here are the available constructors:
SimpleDateFormat()
: Creates a SimpleDateFormat object with the default pattern “EEE MMM dd HH:mm:ss zzz yyyy” and the default locale.SimpleDateFormat(String pattern)
: Creates a SimpleDateFormat object with the specified pattern string and the default locale.SimpleDateFormat(String pattern, Locale locale)
: Creates a SimpleDateFormat object with the specified pattern string and the specified locale.SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
: Creates a SimpleDateFormat object with the specified pattern string and the specified date format symbols.SimpleDateFormat(String pattern, TimeZone timeZone)
: Creates a SimpleDateFormat object with the specified pattern string and the specified time zone.SimpleDateFormat(String pattern, Locale locale, DateFormatSymbols formatSymbols)
: Creates a SimpleDateFormat object with the specified pattern string, locale, and date format symbols.
In these constructors, the pattern
parameter is a string that specifies the format of the date and time. The locale
parameter specifies the locale to use for formatting, while the formatSymbols
parameter specifies the date format symbols to use.
The TimeZone
parameter is used to specify the time zone for the date and time. If not specified, the default time zone of the system is used.
Note that the pattern string used in the constructors is made up of special characters that represent different parts of the date and time, such as year, month, day, hour, minute, and second. The format of the string is important and must match the format of the date and time being parsed or formatted.
Java SimpleDateFormat Example: Date to String
Here is an example of using the SimpleDateFormat class in Java to format a Date object as a String:
import java.text.SimpleDateFormat; import java.util.Date; public class DateToStringExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String formattedDate = formatter.format(date); System.out.println("Formatted Date: " + formattedDate); } }
In this example, we first create a new Date object representing the current date and time. We then create a SimpleDateFormat object with the pattern “dd/MM/yyyy HH:mm:ss”, which specifies the format of the date to be day/month/year hour:minute:second. We then use the format() method of the SimpleDateFormat object to format the date as a String. Finally, we print the formatted date to the console.
The output of this program will be something like:
Formatted Date: 07/05/2023 10:30:15
This shows how to format a Date object as a String using a specific pattern with the SimpleDateFormat class in Java.
Java SimpleDateFormat Example: String to Date
Here is an example of using the SimpleDateFormat class in Java to parse a String into a Date object:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample { public static void main(String[] args) { String dateString = "07/05/2023 10:30:15"; SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); try { Date date = formatter.parse(dateString); System.out.println("Parsed Date: " + date); } catch (ParseException e) { System.out.println("Unable to parse date: " + dateString); e.printStackTrace(); } } }
In this example, we start with a String representing a date and time in the format “dd/MM/yyyy HH:mm:ss”. We then create a SimpleDateFormat object with the same pattern string. We use the parse() method of the SimpleDateFormat object to parse the String into a Date object.
Note that the parse() method throws a ParseException if the input string is not in the expected format. Therefore, we wrap the call to parse() in a try-catch block to handle any possible exceptions.
Finally, we print the parsed Date object to the console. The output of this program will be something like:
Parsed Date: Sun May 07 10:30:15 UTC 2023
This shows how to use the SimpleDateFormat class to parse a String into a Date object using a specific pattern in Java.
Methods:
set2DigitYearStart():
The set2DigitYearStart()
method is a part of the SimpleDateFormat
class in Java. It is used to set the date that marks the beginning of the 100-year period that two-digit year values are interpreted as being within.
There are two overloaded versions of this method:
void set2DigitYearStart(Date startDate)
: Sets the start date of the 100-year period that two-digit year values are interpreted as being within, based on the specified Date object.void set2DigitYearStart(String startDateString)
: Sets the start date of the 100-year period that two-digit year values are interpreted as being within, based on the specified date string. The string should be in the format “mm/dd/yy”.
Here is an example of using the set2DigitYearStart()
method:
import java.text.SimpleDateFormat; import java.util.Date; public class Set2DigitYearStartExample { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); sdf.set2DigitYearStart(new Date(0)); String dateStr = "01/01/21"; try { Date date = sdf.parse(dateStr); System.out.println(date); } catch (Exception e) { e.printStackTrace(); } } }
In this example, we first create a SimpleDateFormat
object with the pattern “MM/dd/yyyy”. We then use the set2DigitYearStart()
method to set the start date of the 100-year period to January 1, 1900 (represented by the Date(0)
constructor).
We then try to parse the string “01/01/21” (which represents January 1, 2021) using the parse()
method of the SimpleDateFormat
object. Since we have set the 2-digit year start to January 1, 1900, the year “21” in the input string will be interpreted as “2021” instead of “1921”. The output of this program will be:
Fri Jan 01 00:00:00 UTC 2021
This shows how to use the set2DigitYearStart()
method to set the start date for interpreting two-digit year values in Java.
get2DigitYearStart():
The get2DigitYearStart()
method is a part of the SimpleDateFormat
class in Java. It is used to get the date that marks the beginning of the 100-year period that two-digit year values are interpreted as being within.
The method signature is as follows:
public Date get2DigitYearStart()
This method returns a Date object representing the start date of the 100-year period that two-digit year values are interpreted as being within.
Here is an example of using the get2DigitYearStart()
method:
import java.text.SimpleDateFormat; import java.util.Date; public class Get2DigitYearStartExample { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Date startDate = sdf.get2DigitYearStart(); System.out.println(startDate); } }
In this example, we first create a SimpleDateFormat
object with the pattern “MM/dd/yyyy”. We then use the get2DigitYearStart()
method to get the start date of the 100-year period for interpreting two-digit year values. We then print this date to the console.
The output of this program will be something like:
Sun Jan 01 00:00:00 UTC 1928
This shows how to use the get2DigitYearStart()
method to get the start date for interpreting two-digit year values in Java.
toLocalizedPattern():
The toLocalizedPattern()
method is a part of the SimpleDateFormat
class in Java. It is used to get the localized pattern string used by the SimpleDateFormat
object.
The method signature is as follows:
public String toLocalizedPattern()
This method returns a string representing the localized pattern used by the SimpleDateFormat
object.
Here is an example of using the toLocalizedPattern()
method:
import java.text.SimpleDateFormat; import java.util.Locale; public class ToLocalizedPatternExample { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", new Locale("fr")); String localizedPattern = sdf.toLocalizedPattern(); System.out.println(localizedPattern); } }
In this example, we first create a SimpleDateFormat
object with the pattern “yyyy/MM/dd” and the French locale. We then use the toLocalizedPattern()
method to get the localized pattern string used by this SimpleDateFormat
object. We then print this pattern string to the console.
The output of this program will be:
"yyyy/MM/dd"
This shows how to use the toLocalizedPattern()
method to get the localized pattern string used by a SimpleDateFormat
object in Java.