Java Certification Training Course
- 45k Enrolled Learners
- Weekend
- Live Class
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,
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
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,
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 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,
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.