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
Using Format
DateFormat class has a format method which is responsible for formatting.
Example
Locale locale = new Locale(“fr”, “FR”);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);
This code will format the date in the French (fr) language and the France (FR) region:
Output:
3 janv. 2018
Using get time Instance()
For creating a DateFormat instance we are using getDateInstance() method.
For performing a time format, we need an instance of time. We will be using getTimeInstance() method for getting an instance of time.
Example
Locale locale = new Locale(“fr”, “FR”);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);
This code will format the time in the French (fr) language and the France (FR) region:
Output
11:03:01
Patterns
Let us have a look at the pattern syntax that should be used for the formatting pattern.
Letter for Pattern | Date or Time component | Examples |
G | Era designator | AD |
y | Year | 2018 (yyyy), 18 (yy) |
M | Month in year | July (MMMM), Jul (MMM), 07 (MM) |
w | Results in week in year | 16 |
W | Results in week in month | 3 |
D | Gives the day count in the year | 266 |
d | Day of the month | 09 (dd), 9(d) |
F | Day of the week in month | 4 |
E | Day name in the week | Tuesday, Tue |
u | Day number of week where 1 represents Monday, 2 represents Tuesday and so on | 2 |
a | AM or PM marker | AM |
H | Hour in the day (0-23) | 12 |
k | Hour in the day (1-24) | 23 |
K | Hour in am/pm for 12 hour format (0-11) | 0 |
h | Hour in am/pm for 12 hour format (1-12) | 12 |
m | Minute in the hour | 59 |
s | Second in the minute | 35 |
S | Millisecond in the minute | 978 |
z | Timezone | Pacific Standard Time; PST; GMT-08:00 |
Z | Timezone offset in hours (RFC pattern) | -0800 |
X | Timezone offset in ISO format | -08; -0800; -08:00 |
Some letters should be used in different amount for different results like for month:
Type | Pattern | Example Output |
Full Month | MMMM | July |
Abbreviated Month | MMM | Jul |
Numeric Month | MM | 07 |
Examples
Let us now look at some examples for different formats of date and time.
Pattern | Result |
---|---|
MM/dd/yyyy | Vector is synchronized. |
dd-M-yyyy hh:mm:ss | Vector is slow as it is thread safe. |
dd MMMM yyyy | 02 January 2018 |
dd MMMM yyyy zzzz | 02 January 2018 India Standard Time |
E, dd MMM yyyy HH:mm:ss z | Tue, 02 Jan 2018 18:07:59 IST |
Using parse()
Parsing is the conversion of String into a java.util.Date instance. We can parse a string to a date instance using parse() method of the SimpleDateFormat class. For parsing a String to Date we need an instance of the SimpleDateFormat class and a string pattern as input for the constructor of the class.
Example
String pattern = “MM-dd-yyyy”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse(“12-01-2018”);
System.out.println(date);
This will output a date (12-01-2018) without a specified time:
Output
Sat Dec 01 00:00:00 IST 2018
Now let’s look at SimpleDateFormat example to parse time.
Example
String pattern = “HH:mm:ss”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse(“22:00:03”);
System.out.println(date);
This will output a time (22:00:03) without a specified date:
Output
Thu Jan 01 22:00:03 IST 1970
Because we have not specified any date the program considered epoch as the date (i.e., 01-Jan-1970).
Using Locale
We have worked with Locale as part of the DateFormat and we have seen that locales are used based on regions.
Example
String pattern = “EEEEE MMMMM yyyy HH:mm:ss.SSSZ”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale(“fr”, “FR”));
Date date = simpleDateFormat.format(new Date());
System.out.println(date);
This code will format the current time in the French (fr) language and the France (FR) region:
Output
mardi janvier 2018 14:51:02.354+0530
The day and month are named in French based on the Locale provided as input – mardi is “Tuesday” and janvier is “January”.
These methods are used to format and parse dates in the programming language of java.
Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka
This Edureka Java Full Course will help you understand the various fundamentals of Java programming.
FAQs
How to write date format in Java?
Java’s date format Can be easily formatted by implementing a DateFormate class. A specified date can be formatted into the Data/Time string. The format should be mm/dd/yyyy.
How do you set dates in Java?
According to the Java API, we can set the date using the constructor Date(year, month, day).
How do you format the date to dd-mm-yyyy in Java?
To Convert the date to dd-mm-yyyy in Java, you use the Java—simpleDateFormat class.
How to write dates in dd-mm-yyyy format?
To write the dates in the dd-mm-yyyy format, you can use the
Java—simpleDateFormat class.
Import java.time.LocalDate; Public class main{ Public static void main(String[] args){ LocalDate myobj=LocalDate.now(); system.out. println(myobj);}}
How do you convert any date format to yyyy-mm-dd in Java?
Converting any date format in java is very easy to format yyyy-mm-dd in Java. You can use the DateFormat class and java.text.simpleDateFormat.
Import java.time.LocalDateTime; LocalDateTime class Import java.time.format.DateTimeFormatter; Import the DateTimeFormatter class
Public class main{ Public static void main(String[] args){ LocalDateTime myDateObj=LocalDateTime.now(); system.out.println(“Before formatting: ”+ myDateObj); DateTimeFormatter myFormatObj=DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss”); String formattedDate=myDateObj.format(myFormatObj); System.out.println(“After formatting: ”+formattedDate); } }
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 Course Online 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.
If you want to start a career in the Node JS Field then check out the Node JS Training Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
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.