All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Date Time Formats Java

Last Updated : Aug 7, 2023

Date Time Formats Java

In this article we will show you the solution of date time formats java, formatting dates in Java is achieved using the SimpleDateFormat and DateFormat classes. Java's date and time functionality is mostly utilized when displaying or utilizing date and time information.

Classes such as these are available within the com.text package.

A date can be formatted as a String based on a locale provided as input using the DateFormat method.

In order to make the code more local to the user, the locale specifies the region and language.

There are several ways in which dates are written around the world. DateFormat classes aren't synchronized, so it's best to create a separate instance for each thread.

We will now discuss the idea of how to create date and time formats in java with an example.

Step By Step Guide On Date Time Formats Java :-

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class TalkersCode{
public static void main(String[] args) {
    Date date = new Date();
    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("E, dd MMM yyyy HH:mm:ss z");
    strDate = formatter.format(date);
    System.out.println("Date format, dd MMM yyyy HH:mm:ss z : "+strDate);
}
}
  1. We import the necessary packages for Date and SimpleDateFormat classes.
  2. We define a public class called TalkersCode.
  3. We define the main method where the code execution starts.
  4. We create an instance of the Date class to get the current date.
  5. We create an instance of the SimpleDateFormat class with the format "MM/dd/yyyy" to format the date in the desired way.
  6. We use the format() method of the SimpleDateFormat class to format the date object.
  7. We store the formatted date string in a variable called strDate.
  8. In this step, we print out the formatted date string to the console.
  9. We repeat steps 5-8 with different format strings to generate and print different date formats to the console.
  10. The program ends when all date formats have been printed to the console.

Conclusion :-

As a result, we have successfully learned how to create date and time formats in java with an example.

Any programming language must take into account the time and date since our computers apply a date and time stamp to all files we create and modify, including documents we save from our applications and email messages we send.

I hope this article on date time formats java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪