VOOZH about

URL: https://www.geeksforgeeks.org/java/java-simpledateformat-set-1/

⇱ Java SimpleDateFormat | Set 1 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java SimpleDateFormat | Set 1

Last Updated : 23 Jul, 2025

SimpleDateFormat class helps in formatting and parsing of data. We can change date from one format to other. It allows to user to interpret string date format into a Date object. We can modify Date accordingly, we want.

Declaration : 

public class SimpleDateFormat
extends DateFormat

Constructors :  

  • SimpleDateFormat(String pattern_arg) : Constructs a Simple Date Format using the given pattern - pattern_arg, default date format symbols for the default FORMAT locale.
  • SimpleDateFormat(String pattern_arg, Locale locale_arg) : Constructs a Simple Date Format using the given pattern - pattern_arg, default date format symbols for the given FORMAT Locale - locale_arg.
  • SimpleDateFormat(String pattern_arg, DateFormatSymbols formatSymbols) : Constructs a SimpleDateFormat using the given pattern - pattern_arg and date format symbols.

Java Program illustrating SimpleDateFormat class  

Output: 

Present Date : Wed Jun 21 18:21:13 IST 2017
Date formatted : 21 / 06 / 17

Methods :  

1. set2DigitYearStart() : java.text.SimpleDateFormat.set2DigitYearStart(Date starting_Date) parses the date and set the date in the range starting_Date to starting_Date + 100 years. 

Syntax :

public void set2DigitYearStart(Date starting_Date)
Parameters :
starting_Date : Date is set in the range - starting_Date to starting_Date + 100 years
Return :
Returns void

Output : 

Initial Time : Thu Oct 27 00:00:00 IST 2016
New Time : Mon Jun 12 00:00:00 IST 1916

2. get2DigitYearStart() : java.text.SimpleDateFormat.get2DigitYearStart() returns start of 100 year period that was set during parsing. 

Syntax :

public void get2DigitYearStart()
Parameters :
-----
Return :
Returns start of 100 year period that was set during parsing.

Implementation : 

Output : 

Initial Time : Thu Oct 27 00:00:00 IST 2016
New Time : Mon Jun 12 00:00:00 IST 1916
START Year : 1900

3. toPattern() : java.text.SimpleDateFormat.toPattern() returns pattern of the Date format. 

Syntax :

public String toPattern()
Parameters :
-----
Return :
Returns pattern of the Date format.

Output : 

Current Date : 6/21/17 6:24 PM
Date Pattern : M/d/yy h:mm a

4. parse() : java.text.SimpleDateFormat.parse() parses text from a string to form Date. It is specified by parse in class SimpleDateFormat. 

Syntax :

public Date parse()
Parameters :
-----
Return :
Returns Date parsed from a string.

Output : 

Time parsed from String : Thu Oct 27 00:00:00 IST 2016

5. applyPattern() : java.text.SimpleDateFormat.applyPattern(String arg) is used to set a defined pattern to the Date Format. 

Syntax :

public void applyPattern(String arg)
Parameters :
arg : defined pattern to be set to the Date Format.
Return :
Void

Implementation : 

Output : 

Current Date : 21 / 06 / 2017 18:25 +0530
Pattern applied : dd / MM / yyyy HH:mm Z

6. format() : java.text.SimpleDateFormat.format(Date arg) changes Date or Time to string.

 Syntax :

public final String format(Date arg)
Parameters :
arg : Date to be formatted to String
Return :
Formatted String of Date

Output : 

Actual Date : Wed Jun 21 18:25:50 IST 2017
Formatted Date to String : 6/21/17 6:25 PM

7. toLocalizedPattern() : java.text.SimpleDateFormat.toLocalizedPattern() returns Date pattern String of the Date Formatter. 

Syntax :

public String toLocalizedPattern()
Parameters :
------
Return :
Date pattern String used in the formatter

Output : 

Date : 6/21/17 6:27 PM
Pattern in DateFormater 'geek' : M/d/yy h:mm a

Next: Java.text.SimpleDateFormat class | Set 2 


 

Comment
Article Tags: