SimpleDateFormat class in Java With Examples
java.text.SimpleDateFormat is the subclass of the DateFormat abstract class.
This is a concrete class for formatting and parsing dates. The SimpleDateFormat class provides the methods for formatting (conversion from date to text), parsing (conversion from text to date), and normalization in a locale-sensitive manner.
We can choose user-defined patterns for formatting, but it is good practice to create date-time formatter by using getDateInstance(), getTimeInstance() or getDateTimeInstance() upon DateFormat object.
These methods can return a date-time formatter initialized with a by default format pattern.
The format pattern can be modified by using applyPattern() methods later according to the requirement.
SimpleDateFormat Constructors
SimpleDateFormat()
Default constructor with a default pattern, default date format symbols and default locale.
SimpleDateFormat(String pattern)
Creates a new SimpleDateFormat object with a specified pattern, default date format symbols, and default locale.
SimpleDateFormat(String pattern, DateFormatSymbols dfs)
Creates an object of SimpleDateFormat with specified pattern and date format symbols and default locale.
SimpleDateFormat(String pattern, Locale locale)
Creates an object of SimpleDateFormat with input pattern and the default date format symbols for the given locale.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");//M represents Month and m represents Minute
String dateNow= sdf.format(date);
System.out.println("Date
and Time right now:"+dateNow);
}
}
Output:
Date and Time right now:05/04/2020 02:49:17
Date and time patterns
The pattern strings are used to specify the format of the date and time. The unquoted alphabets from A-Z and a-z are used to represent the components of the date and time strings. All other alphabets are not interpreted, they are used as they were in the input string. (unused alphabets are reserved)Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote.
Few of the pattern letter and their representations are listed below,
Alphabet |
Date or Time representation |
d | Date |
M | Month |
y | Year |
H | Hour in day (0-23) |
h | Hour in AM/PM(1-12) |
m | Minute |
s | Second |
S | Milli-second |
z | Time-zone |
G | ERA(example AD) |
w | Week in the year |
W | Week in the month |
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("E, dd-MMM-yyyy HH:mm:ss z
G");
String dateNow = sdf.format(date);
System.out.println("Date
and Time right now:"+dateNow);
System.out.println("Formatted
date with pattern ||E, dd-MMM-yyyy HH:mm:ss z G||\n"+dateNow);
}
}
Output:
Date and Time right now:Sun, 05-Apr-2020 15:34:25 IST AD
Formatted date with pattern ||E, dd-MMM-yyyy HH:mm:ss z G||
Sun, 05-Apr-2020 15:34:25 IST AD
Formatted date with pattern ||E, dd-MMM-yyyy HH:mm:ss z G||
Sun, 05-Apr-2020 15:34:25 IST AD
The parse() method
The parse method is used to parse a text string into a date. the method returns a Date object and throws ParseException if an invalid or not parseable string is provided as input.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
String str="05/Apr/2020 04:12:45";
SimpleDateFormat sdf=new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");
try {
Date datenow=sdf.parse(str);
System.out.println("Time
right now:"+datenow);
} catch (ParseException e) {
System.out.println("invalid
input");
e.printStackTrace();
}
}
}
Output:
Time right now:Sun Apr 05 04:12:45 IST 2020
Reference:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html