Java SimpleDateFormatThe java.text.SimpleDateFormat class provides methods to format and parse date and time in java. The SimpleDateFormat is a concrete class for formatting and parsing date which inherits java.text.DateFormat class. Notice that formatting means converting date to string and parsing means converting string to date. Constructors of the Class SimpleDateFormatSimpleDateFormat(String pattern_args): Instantiates the SimpleDateFormat class using the provided pattern - pattern_args, default date format symbols for the default FORMAT locale. SimpleDateFormat(String pattern_args, Locale locale_args): Instantiates the SimpleDateFormat class using the provided pattern - pattern_args. For the provided FORMAT Locale, the default date format symbols are - locale_args. SimpleDateFormat(String pattern_args, DateFormatSymbols formatSymbols): Instantiates the SimpleDateFormat class and using the provided pattern - pattern_args and the date formatSymbols. Java SimpleDateFormat Example: Date to StringLet's see the simple example to format date in java using java.text.SimpleDateFormat class. FileName: SimpleDateFormatExample.java Test it NowOutput: 13/04/2015 Note: M (capital M) represents month and m (small m) represents minute in java.Let's see the full example to format date and time in java using java.text.SimpleDateFormat class. FileName: SimpleDateFormatExample2.java Test it NowOutput: Date Format with MM/dd/yyyy : 04/13/2015 Date Format with dd-M-yyyy hh:mm:ss : 13-4-2015 10:59:26 Date Format with dd MMMM yyyy : 13 April 2015 Date Format with dd MMMM yyyy zzzz : 13 April 2015 India Standard Time Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 13 Apr 2015 22:59:26 IST Java SimpleDateFormat Example: String to DateLet's see the simple example to convert string into date using java.text.SimpleDateFormat class. FileName: SimpleDateFormatExample3.java Test it NowOutput: Date is: Tue Mar 31 00:00:00 IST 2015 Methodsset2DigitYearStart()Syntax: Parameters: startDate: The date has been set in the range - startDate to startDate + 100 years Return Type: The method return type is void Implementation: Let's see how one can implement the method in the code. FileName: Set2DigitYearStart.java Output: Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 The New Timing is : Mon Feb 02 00:00:00 GMT 2015 get2DigitYearStart()Syntax: Parameters: No parameter is required for this method Return Type: The method return type is Date and returns the beginning of the 100 years period that was being set during the parsing. Implementation: Let's see how one can implement the method in the code. FileName: Get2DigitYearStart.java Output: Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 The New Timing is : Mon Feb 02 00:00:00 GMT 2015 The start year is: 2000 toPattern()Syntax: Parameters: No parameter is required for this method Return Type: The method return type is String and returns the date format pattern. Implementation: Let's see how one can implement the method in the code. FileName: ToPattern.java Output: Current Date : 12/11/21, 7:24 AM The Date Pattern is: M/d/yy, h:mm a parse()Syntax: Parameters: No parameter is required for this method Return Type: The method return type is Date and returns the date parsed. Implementation: Let's see how one can implement the method in the code. FileName: Parse.java Output: Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 applyPattern()Syntax: Parameters: No parameter is required for this method Return Type: The method return type is void. Hence, the method returns nothing. Implementation: Let's see how one can implement the method in the code. FileName: ApplyPattern.java Output: The current date is: 11 / 12 / 2021 07:41 +0000 The Pattern applied is: dd / MM / yyyy HH:mm Z format()Syntax: Parameters: The method takes Date as the argument Return Type: The method return type is String, and the method returns the formatted string of the date. Implementation: Let's see how one can implement the method in the code. FileName: Format.java Output: The actual date is: Sat Dec 11 13:48:36 GMT 2021 The formatted date is: 12/11/21, 1:48 PM toLocalizedPattern()Syntax: Parameters: The method does not take any argument Return Type: The method return type is String, and the method returns the Date pattern string of the date formatter. Implementation: Let's see how one can implement the method in the code. FileName: ToLocalizedPattern.java Output: The Date is: 12/11/21, 3:01 PM The pattern in the DateFormater is : M/d/yy, h:mm a
Next TopicJava Get Current Date and Time in Java
|