Core JAVA

Convert Date to String in Java with Example

Date is a class in java.util package in java. It represent date of the day with time stamp. But in the application we need to display the date upfront in the String format. So it is very common requirement to convert Date to String. So we need to convert java.util.Date to string in java for displaying purpose. In there are various way of converting Date to String by using SimpleDateFormat class in java. Actually there are two classes in java.text package DateFormat and SimpleDateFormat and they are very powerful and you can use one of them for Date to String conversion.

Converting Date to String in Java

Here we are discussing an example of converting Date to String by using SimpleDateFormat. Let’s see following steps:
  • Step 1: Create a valid date format using SimpleDateFormat as you wanna display upfront
  • Step 2: Then call format() method of SimpleDateFormat and passed Date object as argument of this method, it will return String format of date as you specified in the first step.


For Example:

public static void main(String[] args) {
  //Creating Date in java with today's date.
  Date todayDate = new Date();

  //change date to string on dd-MM-yyyy format e.g. "20-02-2017"
  SimpleDateFormat dateformater_Java = new SimpleDateFormat("dd-MM-yyyy");
  String todayDateStr = dateformater_Java.format(todayDate);
  System.out.println("Today's date into dd-MM-yyyy format: " + todayDateStr);
  
  //converting date to string dd/MM/yyyy format for example "20/02/2017"
  SimpleDateFormat formatDateJava = new SimpleDateFormat("dd/MM/yyyy");
  todayDateStr = formatDateJava.format(todayDate);
  System.out.println("Today's date into dd/MM/yyyy format: " + todayDateStr);
    
  //change date into string yyyyMMdd format example "20170220"
  SimpleDateFormat dateformater_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
  todayDateStr = dateformater_yyyyMMdd.format(todayDate);
  System.out.println("Today's date into yyyyMMdd format: " + todayDateStr);

  //converting  date into ddMMyyyy format example "20022017"
  SimpleDateFormat dateformater_ddMMyyyy = new SimpleDateFormat("ddMMyyyy");
  todayDateStr = dateformater_ddMMyyyy.format(todayDate);
  System.out.println("Today's date into ddMMyyyy format: " + todayDateStr);
    
  //date to dd-MMM-yy format e.g. "20-Feb-17"
  SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd-MMM-yy");
  todayDateStr = ddMMMyyFormat.format(todayDate);
  System.out.println("Today's date into dd-MMM-yy format: " + todayDateStr);

  //convert date to dd-MMMM-yy format e.g. "20-August-17"
  SimpleDateFormat ddMMMMyyFormat = new SimpleDateFormat("dd-MMMM-yy");
  todayDateStr = ddMMMMyyFormat.format(todayDate);
  System.out.println("Today's date into dd-MMMM-yy format: " + todayDateStr);
 }

Output:
Today’s date into dd-MM-yyyy format: 20-02-2017
Today’s date into dd/MM/yyyy format: 20/02/2017
Today’s date into yyyyMMdd format: 20170220
Today’s date into ddMMyyyy format: 20022017
Today’s date into dd-MMM-yy format: 20-Feb-17
Today’s date into dd-MMMM-yy format: 20-February-17

SimpleDateFromat Java Doc
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

SimpleDateFormat is an excellent utility for converting String to Date and then Date to String. It is not thread-safe so its better you create separate DateFormat for each thread to avoid any race condition while parsing date in java.

There are some designator of SimpleDateFormat
y -> Year Year 1996; 96
M -> Month in year Month July; Jul; 07
w -> Week in year Number 27
W -> Week in month Number 2
D -> Day in year Number 189
d -> Day in month Number 10
a -> Am/pm marker Text PM
H -> Hour in day (0-23) Number 0
h -> Hour in am/pm (1-12) Number 12
m -> Minute in hour Number 30
s -> Second in minute Number 55
S -> Millisecond Number 978

Summary
In this tutorial we have seen how to convert Date to String. Format of date in String is depends to the developer but developer can use predefined pattern letters. We careful about like “m” and “M”, there are some letter are small case and same letter also available in capital case so they confused to developer some time.

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago