SimpleDateFormat

Share

SimpleDateFormat

SimpleDateFormat is a class in Java that allows you to format and parse dates and times according to a specified pattern. It is part of the java.text package and provides a simple way to work with date and time representations as strings. SimpleDateFormat is often used when you need to display dates and times in a human-readable format or when you need to parse user-input dates into a standardized format.

Here’s how you can use SimpleDateFormat:

1. Formatting Dates:

To format a Date object into a string representation, you create a SimpleDateFormat instance with the desired date pattern and then use the format method:

java
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date currentDate = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); String formattedDate = dateFormat.format(currentDate); System.out.println("Formatted Date: " + formattedDate); } }

In this example, the pattern "dd-MM-yyyy HH:mm:ss" specifies the format for the date and time, and dateFormat.format(currentDate) converts the Date object into a string using that pattern.

2. Parsing Dates:

To parse a date string into a Date object, you create a SimpleDateFormat instance with the pattern matching the input date format and then use the parse method:

java
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateParseExample { public static void main(String[] args) { String dateStr = "20-12-2023 15:30:00"; SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); try { Date parsedDate = dateFormat.parse(dateStr); System.out.println("Parsed Date: " + parsedDate); } catch (ParseException e) { e.printStackTrace(); } } }

In this example, the pattern "dd-MM-yyyy HH:mm:ss" matches the format of the input date string, and dateFormat.parse(dateStr) converts the string into a Date object.

3. Pattern Elements:

SimpleDateFormat patterns use various letters and symbols to represent date and time components. Here are some common pattern elements:

  • y: Year (e.g., “yyyy” for 4-digit year).
  • M: Month in year (e.g., “MM” for 2-digit month).
  • d: Day in month (e.g., “dd” for 2-digit day).
  • H: Hour in day (0-23).
  • m: Minute in hour (e.g., “mm” for 2-digit minute).
  • s: Second in minute (e.g., “ss” for 2-digit second).
  • E: Day name in week (e.g., “EEE” for abbreviated day name).

You can combine these elements with custom separators and additional text to create your desired date and time formats.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

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