import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateFormatExample2 {
public static void main(String[] args) {
Date date = new Date();
System.out.println("Date format change by TechSarvam ");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String strDate = formatter.format(date);
System.out.println("Date Format with MM/dd/yyyy : " + strDate);
formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
strDate = formatter.format(date);
System.out.println("Date Format with dd-M-yyyy hh:mm:ss : " + strDate);
formatter = new SimpleDateFormat("dd MMMM yyyy");
strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy : " + strDate);
formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");
strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy zzzz : " + strDate);
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
strDate = formatter.format(date);
System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : " + strDate);
}
}
Program Explanation:
This program demonstrates how to use the SimpleDateFormat class in Java to format the current date into different patterns. Let’s go through the program step by step:
Explanation of Key Elements:
1. Date date = new Date();:
- It creates a Date object representing the current date and time.
- The Date constructor without arguments initializes the object with the current date and time based on the system’s clock.
2. SimpleDateFormat formatter = new SimpleDateFormat(pattern);:
SimpleDateFormat is a concrete subclass of DateFormat which enables you to format and parse dates according to specific patterns.
The used patterns here are:
- MM/dd/yyyy: Month/Day/Year in numerical format.
- dd-M-yyyy hh:mm:ss: Day-Month-Year Hour:Minute:Second in 12-hour format.
- dd MMMM yyyy: Day Month (full month name) Year.
- MMMM dd yyyy zzzz: Day of the Week. Month (in words).Year Time Zone
- E, dd MMM yyyy HH:mm:ss z:Day of the week, Day month year hour :minute : seconds time zone.
3. Formatter.format(date)
This approach prints the date in a format specified in SimpleDateFormat object in String form of representation.
4.System.out.println(strDate);
It prints the formatted date string to the console.
Date Formatting Patterns Used
1. MM/dd/yyyy
- Pattern Explanation: Month (2 digits)/Day (2 digits)/Year (4 digits)
- Example: 12/31/2022
2. dd-M-yyyy hh:mm:ss:
- Pattern Explanation: Day (2 digits)-Month (1 or 2 digits)-Year (4 digits) Hour (12-hour format):Minute:Second
- Example: 31-12-2022 03:45:30
3. dd MMMM yyyy:
- Pattern Explanation: Day (2 digits) Full Month Name (e.g., January) Year (4 digits)
- Example: 31 December 2022
4. dd MMMM yyyy zzzz:
- Pattern Explanation: Day (2 digits) Full Month Name (e.g., December) Year (4 digits) Time Zone (full name)
- Example: 31 December 2022 PST depends on the system’s time zone
5. E, dd MMM yyyy HH:mm:ss z:
- Pattern Explanation: Day of the Week (e.g., Mon), Day Month Year Hour:Minute:Second Time Zone
- Example: Sat, 31 Dec 2022 15:45:30 PST
Program Output:
After running the program, the output will appear something like this, assuming the date is 31st December 2022 and your system’s time zone is PST,
Date format change by TechSarvam
Date Format with MM/dd/yyyy : 12/31/2022
Date Format with dd-M-yyyy hh:mm:ss : 31-12-2022 03:45:30
Date Format with dd MMMM yyyy : 31 December 2022
Date Format with dd MMMM yyyy zzzz : 31 December 2022 PST
Date Format with E, dd MMM yyyy HH:mm:ss z : Sat, 31 Dec 2022 15:45:30 PST
Explanation of Output:
- MM/dd/yyyy returns the date in Month/Day/Year format.
- dd-M-yyyy hh:mm:ss returns the date in Day-Month-Year Hour:Minute:Second format, in 12-hour format.
- dd MMMM yyyy gives the day, full month name, and year.
- dd MMMM yyyy zzzz gives the full month name, date, year, and time zone.
- E, dd MMM yyyy HH:mm:ss z gives the day of the week, day, month, year, 24-hour time format, and time zone.
Conclusion:
This program will demonstrate how to print the current date and time in Java using SimpleDateFormat with several formatting patterns. Each of these patterns differs in the way the date is printed and thus can be adapted for use to display dates any way you may need.