Class SimpleDateFormat in Java

digitization, transformation, binary

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.

Ads code goes here

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 FiledFull PatternShort PatternExampleType
Yearyyyy (4 digits)yy (2 digits)2019Numeric
MonthMMM (Name)MM (2 digits)
M   (1/2 digits)
3 or MarchText or numeric
Day of WeekEEEEEE(E)ThursdayText
Day of the Monthdd (2 digits)d (1/2 digits)4Numeric
Day of week in a monthFF1Numeric
Day in yearDDD (3 digits)D (1/2/3 digits)55Numeric
Week in yearwww4Numeric
Week in MonthWWW1Numeric
EraGADText
TimezonezzzzzzESTText
AM/PMaa2 P.M.Text
Hours in AM/PMKK2Numeric
Hours [ 0-12]hh (2 digits)h (1/2 digits)2Numeric
Hours [ 0-23]HH (2 digits)H (1/2 digits)14Numeric
Hours [0-11]KKK2Numeric
Hours [1-24]kkk14Numeric
Minutes in hourmmm55Numeric
Seconds in minutessss22Numeric
MillisecondsSSSS444Numeric
READ  All About Class RandomAccessFile in Java

 

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.

READ  Class Compiler In Java

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.

READ  Class Class in Java

This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.

Parameter
date – The Date object to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
pos – A date or time field.

public DateFormatSymbols getDateFormatSymbols();

public DateFormatSymbols getDateFormatSymbols() method returns the DateFormatSymbols object that this SimpleDateFormat uses internally.

This method returns the DateFormatSymbols object used by this SimpleDateFormat.

public int hashCode(); 

public int hashCode(); method returns a hashcode for this SimpleDateFormat.

This method returns a hashcode for this object.

public Date parse(String text, ParsePosition pos);

public Date parse(String text, ParsePosition pos) method parses a date from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.

This method returns the Date object represented by the text starting at the given position.

Parameter
text – The string to be parsed.
pos – A ParsePosition object that specifies a position in the string.

public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols);

public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols) method sets the DateFormatSymbols object that this SimpleDateFormat uses internally.

Parameter
newFormatSymbols – The new DateFormatSymbols object to use.

public String toLocalizedPattern();

public String toLocalizedPattern() method returns the pattern string of this SimpleDateFormat, localized with the DateFormatSymbols object of this SimpleDateFormat.

This method returns the pattern string of this SimpleDateFormat.

public String toPattern();

public String toPattern() method returns the pattern string of this SimpleDateFormat

This method returns the pattern string of this SimpleDateFormat.

Apart from these methods the SimpleDateFormat class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)

SimpleDateFormat class also has inherited methods from DateFormat. They are as follows:

  • format(Date)
  • getCalendar()
  • getNumberFormat()
  • isLenient()
  • setCalendar(Calendar)
  • setNumberFormat(NumberFormat)
  • format(Object, StringBuffer,FieldPosition)
  • getTimeZone()
  • parse(String)
  • parseObject(String, ParsePosition)
  • setLenient(boolean)
  • setTimeZone(TimeZone)

SimpleDateFormat class also has inherited methods from Format. They are as follows:

  • parseObject(String)
  • format(Object)

Share and Enjoy !

12 thoughts on “Class SimpleDateFormat in Java”

  1. It is not my first time to pay a visit this web site, i am browsing this web page dailly and obtain fastidious information from here everyday.

  2. Hi! I know this is kinda off topic but I was wondering which blog platform are you using for this
    site? I’m getting sick and tired of WordPress because I’ve had
    issues with hackers and I’m looking at alternatives for another platform.

    I would be fantastic if you could point me in the direction of
    a good platform.

  3. You actually make it seem so easy with your presentation but I find this topic to be
    really something that I think I would never understand.
    It seems too complicated and very broad for me. I’m looking
    forward for your next post, I’ll try to get the hang of it!

  4. I blog frequently and I genuinely thank you for your information. The article has
    really peaked my interest. I am going to bookmark your site and
    keep checking for new information about once a
    week. I opted in for your RSS feed as well.

  5. Hello just wanted to give you a quick heads up and let you know a few of
    the images aren’t loading correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show the same
    outcome.

  6. Link exchange is nothing else but it is only
    placing the other person’s blog link on your page at suitable
    place and other person will also do similar in favor of you.

  7. Pingback: Google

  8. Pingback: Google

  9. Pingback: Google

  10. Pingback: Google

  11. Pingback: Google

  12. Pingback: Google

Leave a Comment

Your email address will not be published. Required fields are marked *