Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Create Date Format In Java

Published on Jul 15,2019 2.2K Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Different programming languages have different ways of dealing with dates. Java uses Date Format to do the same. In this article we would be understanding Date Format in Java in detail. Following are the pointers, this article focuses on,

So let us get started with this article,

Date Format In Java

The DateFormat class in Java is used for formatting dates. A specified date can be formatted into the Data/Time string. For example, a date can be formatted into: mm/dd/yyyy. Date Format classes are not synchronized.

So let us move ahead and see how tom create a Date Format

Creating A Date Format

public final String format(Date date)

This method returns Date or Time in a string format.

Example:


import java.text.*;
import java.util.Calendar;
public class Main {
public static void main(String[] args)
{
//Initializing the date formatter
DateFormat Date = DateFormat.getDateInstance();
//Initializing Calender Object
Calendar cals = Calendar.getInstance();
//Displaying the actual date
System.out.println("The original Date: " + cals.getTime());
//Using format() method for conversion
String currentDate = Date.format(cals.getTime());
System.out.println("Formatted Date: " + currentDate);
}
}

Output:

The original Date: Mon Jul 08 08:21:53 UTC 2019

Formatted Date: Jul 8, 2019

In the next bit of this article on Date Format In Java, we will see how to create a simple date format,

Creating A Simple Date Format

A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner.

String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

The specified parameter “pattern” is the pattern used for formatting and parsing dates.

In the next bit of this article, we will see how to create a simple date format with date to string approach,

Date To String

Date can be formatted in java using the SimpleDateFormat as well:

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat DateFor = new SimpleDateFormat("dd/MM/yyyy");
String stringDate= DateFor.format(date);
System.out.println(stringDate);
}
}

Output:

08/07/2019

In the next bit of this article on Date Format In Java, we will see how to create a simple date format with string to date approach,

String To Date

We use the method of parsing to convert a string to date.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
SimpleDateFormat DateFor = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = DateFor.parse("08/07/2019");
System.out.println("Date : "+date);
}catch (ParseException e) {e.printStackTrace();}
}
}

Output:

Date : Mon Jul 08 00:00:00 UTC 2019

Example:

Let’s look at the complete example with multiple formats:


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat DateFor = new SimpleDateFormat("MM/dd/yyyy");
String stringDate = DateFor.format(date);
System.out.println("Date Format with MM/dd/yyyy : "+stringDate);
DateFor = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
stringDate = DateFor.format(date);
System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+stringDate);
DateFor = new SimpleDateFormat("dd MMMM yyyy");
stringDate = DateFor.format(date);
System.out.println("Date Format with dd MMMM yyyy : "+stringDate);
DateFor = new SimpleDateFormat("dd MMMM yyyy zzzz");
stringDate = DateFor.format(date);
System.out.println("Date Format with dd MMMM yyyy zzzz : "+stringDate);
DateFor = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
stringDate = DateFor.format(date);
System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z :"+stringDate);
}
}

Output:

Date Format with MM/dd/yyyy : 07/08/2019

Date Format with dd-M-yyyy hh:mm:ss : 08-7-2019 08:51:58

Date Format with dd MMMM yyyy : 08 July 2019

Date Format with dd MMMM yyyy zzzz : 08 July 2019 Coordinated Universal Time

Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 08 Jul 2019 08:51:

58 UTC

These methods are used to format and parse dates in the programming language of java.

Thus we have come to an end of this article on ‘Date Format in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.