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
Java
// Java Program illustrating SimpleDateFormat class import java.text.*; import java.util.*; public class NewClass { public static void main(String[] args) { SimpleDateFormat geek = new SimpleDateFormat( "dd / MM / yy" ); // Creating instance of the System date Calendar c = Calendar.getInstance(); System.out.println( "Present Date : " + c.getTime()); // Formatting Date according "dd / MM / yy" String formattedDate = geek.format(c.getTime()); System.out.println( "Date formatted : " +formattedDate); } } |
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
Java
// Java Program illustrating // use of set2DigitYearStart() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat( "MM / dd / yy" ); try { Calendar c = Calendar.getInstance(); c.setTime(geek.parse( "10 / 27 / 16" )); System.out.println( "Initial Time : " +c.getTime()); // Setting 1916 instead of 2016 // Use of set2DigitYearStart() method geek.set2DigitYearStart(geek.parse( "01 / 01 / 1900" )); c.setTime(geek.parse( "06 / 12 / 16" )); System.out.println( "New Time : " +c.getTime()); } catch (ParseException except) { except.printStackTrace(); } } } |
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 :
Java
// Java Program illustrating // use of get2DigitYearStart() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat( "MM / dd / yy" ); try { Calendar c = Calendar.getInstance(); c.setTime(geek.parse( "10 / 27 / 16" )); System.out.println( "Initial Time : " +c.getTime()); // Setting 1916 instead of 2016 // Use of set2DigitYearStart() method geek.set2DigitYearStart(geek.parse( "01 / 01 / 1900" )); // Start Year is 1990. c.setTime(geek.parse( "06 / 12 / 16" )); System.out.println( "New Time : " +c.getTime()); // Use of get2DigitYearStart() method to check start year c.setTime(geek.get2DigitYearStart()); System.out.println( "START Year : " +c.get(Calendar.YEAR)); } catch (ParseException except) { except.printStackTrace(); } } } |
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.
Java
// Java Program illustrating use of toPattern() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat(); // Initializing Calendar object Calendar c = Calendar.getInstance(); // getting Current Date String dateToday = geek.format(c.getTime()); System.out.println( "Current Date : " +dateToday); // Use of toPattern() method // Printing Date Pattern System.out.println( "Date Pattern : " +geek.toPattern()); } } |
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.
Java
// Java Program illustrating // use of parse() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat( "MM / dd / yy" ); try { Calendar c = Calendar.getInstance(); // Use of .parse() method to parse Date From String 's' String s = "10 / 27 / 16" ; c.setTime(geek.parse(s)); System.out.println( "Time parsed from String : " +c.getTime()); } catch (ParseException except) { except.printStackTrace(); } } } |
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 :
Java
// Java Program illustrating // use of applyPattern() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat(); // Initializing calendar Object Calendar c = Calendar.getInstance(); // Using 'arg' pattern String arg = "dd / MM / yyyy HH:mm Z" ; // Use of applyPattern() method to set date to 'arg' format geek.applyPattern(arg); // current date and time String currentdate = geek.format(c.getTime()); System.out.println( "Current Date : " +currentdate); // Print the pattern being used System.out.println( "Pattern applied : " +geek.toPattern()); } } |
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
Java
// Java Program illustrating // use of format() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { SimpleDateFormat geek = new SimpleDateFormat(); // Initializing calendar Object Calendar c = Calendar.getInstance(); System.out.println( "Actual Date : " +c.getTime()); // Use of format() method to format Date to String String currentdate = geek.format(c.getTime()); System.out.println( "Formatted Date to String : " +currentdate); } } |
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
Java
// Java Program illustrating // use of toLocalizedPattern() method import java.text.*; import java.util.Calendar; public class NewClass { public static void main(String[] args) throws InterruptedException { // Date Formatter SimpleDateFormat geek = new SimpleDateFormat(); // Initializing calendar Object Calendar c = Calendar.getInstance(); System.out.println( "Date : " + geek.format(c.getTime())); // Use of format() method to format Date to String System.out.println( "Pattern in DateFormater 'geek' :" + geek.toLocalizedPattern()); } } |
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
This article is contributed by Mohit Gupta_OMG 😀. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.