Class SimpleDateFormat in Java

0
260
Spread the love
  • 14
  • 34
  • 23
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    95
    Shares

Class SimpleDateFormat in Java:

SimpleDateFormat is a subclass of Format class to handle parsing and formatting of dates. We can not use the SimpleDateFormat class directly as the Date may be affected by locale. It is used mostly by DateFormat class by getting the locale info.

The SimpleDateFormat class is a concrete subclass of DateFormat that formats and parses dates and times using a formatting pattern. Typically, we do not need to instantiate SimpleDateFormat ourselves. Instead, the factory methods of DateFormat return instances of SimpleDateFormat that are appropriate for particular locales.

SimpleDateFormat class is helpful to format a specified date and time as per specified patterns for a default locale or predefined locale.

However, if we need a specialized date and time format, we can instantiate our own SimpleDateFormat using a pattern string. We can also modify the formatting pattern of an existing SimpleDateFormat object using the applyPattern() method. The following symbols are significant in the pattern string.

The applyPattern() method of the SimpleDateFormat class is responsible for formatting the pattern used by an object.

The pattern table is given as:

 

On Filed Full Pattern Short Pattern Example Type
Year yyyy (4 digits) yy (2 digits) 2019 Numeric
Month MMM (Name) MM (2 digits)
M   (1/2 digits)
3 or March Text or numeric
Day of Week EEEE EE(E) Thursday Text
Day of the Month dd (2 digits) d (1/2 digits) 4 Numeric
Day of week in a month F F 1 Numeric
Day in year DDD (3 digits) D (1/2/3 digits) 55 Numeric
Week in year ww w 4 Numeric
Week in Month WW W 1 Numeric
Era G AD Text
Timezone zzzz zz EST Text
AM/PM a a 2 P.M. Text
Hours in AM/PM K K 2 Numeric
Hours [ 0-12] hh (2 digits) h (1/2 digits) 2 Numeric
Hours [ 0-23] HH (2 digits) H (1/2 digits) 14 Numeric
Hours [0-11] KK K 2 Numeric
Hours [1-24] kk k 14 Numeric
Minutes in hour mm m 55 Numeric
Seconds in minutes ss s 22 Numeric
Milliseconds SSS S 444 Numeric

 

Symbols that are numeric can be repeated to specify a minimum number of digits. For example, “hh” produces an hour field that is always at least two digits, like “02”. Symbols that are textual can be repeated to specify whether the short form or the long form of the text string is used if there are both short and long forms.

If four or more symbols are specified, the long-form is used; otherwise, the short form is used. For example, “E” produces a short form of the day of the week, such as “Tue”, while “EEEE” produces the long-form, such as “Tuesday”.

For the month of the year, if one or two “M” symbols are used, the field is numeric. If three or more “M” symbols are used, the field is textual. Single quotes can be used to specify literal text that should be included in the formatted output, and any unrecognized symbol is treated as literal text. For example, the following pattern:

hh:mm a 'in the' zzzz 'zone.'

produces output like:
02:33 PM in the Eastern Standard Time zone.

Internally, the SimpleDataFormat class uses a DateFormatSymbols object to get the date and time strings that are appropriate for a particular locale. If we want to modify these strings, we can get the DateFormatSymbols object by calling getDateFormatSymbols().

The Structure of SimpleDateFormat in Java is given as:

public class java.text.SimpleDateFormat extends java.text.DateFormat {
//Constructors
public SimpleDateFormat();
public SimpleDateFormat(String pattern);
public SimpleDateFormat(String pattern, Locale loc);
public SimpleDateFormat(String pattern, DateFormatSymbols formatData);
//Methods
public void applyLocalizedPattern(String pattern);
public void applyPattern(String pattern);
public Object clone(); 
public boolean equals(Object obj); 
public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition pos);
public DateFormatSymbols getDateFormatSymbols();
public int hashCode(); 
public Date parse(String text, ParsePosition pos);
public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols);
public String toLocalizedPattern();
public String toPattern();
} 

The details of the class structure are given as follows:

public SimpleDateFormat();

public SimpleDateFormat() constructor creates a SimpleDateFormat that uses a default formatting pattern and DateFormatSymbols that are appropriate for the default locale. It produces the same result as calling DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).

public SimpleDateFormat(String pattern);

public SimpleDateFormat(String pattern) constructor creates a SimpleDateFormat that uses the given formatting pattern and a DateFormatSymbols object that is appropriate for the default locale.

Parameter
pattern – The pattern string.

public SimpleDateFormat(String pattern, Locale loc);

public SimpleDateFormat(String pattern, Locale loc) constructor creates a SimpleDateFormat that uses the given formatting pattern and a DateFormatSymbols object that is appropriate for the given locale.

Parameter
pattern – The pattern string.
loc – The Locale to use.

public SimpleDateFormat(String pattern, DateFormatSymbols formatData);

public SimpleDateFormat(String pattern, DateFormatSymbols formatData) constructor creates a SimpleDateFormat that uses the given formatting pattern and DateFormatSymbols object.

Parameter
pattern – The pattern string.
formatData – The DateFormatSymbols to use.

public void applyLocalizedPattern(String pattern);

public void applyLocalizedPattern(String pattern) method tells this SimpleDateFormat to use the given formating pattern to format and parse dates and times. The pattern string is assumed to have been localized to the DateFormatSymbols object this SimpleDateFormat uses.

Parameter
pattern – The pattern string.

public void applyPattern(String pattern);

public void applyPattern(String pattern) method tells this SimpleDateFormat to use the given formatting pattern to format and parse dates and times. The pattern string is localized to the DateFormatSymbols object this SimpleDateFormat uses.

Parameter
pattern – The pattern string.

public Object clone(); 

public Object clone(); method creates a copy of this SimpleDateFormat and returns it.

This method returns a copy of this SimpleDateFormat.

public boolean equals(Object obj); 

public boolean equals(Object obj); method returns true if obj is an instance of SimpleDateFormat and is equivalent to this SimpleDateFormat.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object

public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition pos);

public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition pos) method formats the given date and appends the result to the given StringBuffer. If pos refers to one of the time or date fields, its beginning and ending indexes are filled with the beginning and ending positions of the given field in the resulting formatted string.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 14
  • 34
  • 23
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    95
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here