<div dir="ltr" style="text-align: justify;">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 <a href="https://dineshonjava.com/simpledateformat-in-java-format-date-to-string-to-date-with-example/">Date to String by using SimpleDateFormat</a> 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.</div>
<h2 dir="ltr" style="text-align: justify;"><b>Converting Date to String in Java</b></h2>
<div dir="ltr" style="text-align: justify;">Here we are discussing an example of converting Date to String by using SimpleDateFormat. Let&#8217;s see following steps:</p>
<ul style="text-align: left;">
<li><b>Step 1:</b> Create a valid date format using SimpleDateFormat as you wanna display upfront</li>
<li><b>Step 2:</b> 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.</li>
</ul>
<div class="separator" style="clear: both; text-align: center;"><img title="Convert Date to String in Java" src="https://dineshonjava.com/wp-content/uploads/2017/02/Convert-Date-to-String-1.png" border="0" /></div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b><br />
</b> <b>For Example:</b></p>
<pre class="highlight">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); 
 } 
</pre>
<p><b>Output:</b><br />
<i>Today&#8217;s date into dd-MM-yyyy format: 20-02-2017</i><br />
<i>Today&#8217;s date into dd/MM/yyyy format: 20/02/2017</i><br />
<i>Today&#8217;s date into yyyyMMdd format: 20170220</i><br />
<i>Today&#8217;s date into ddMMyyyy format: 20022017</i><br />
<i>Today&#8217;s date into dd-MMM-yy format: 20-Feb-17</i><br />
<i>Today&#8217;s date into dd-MMMM-yy format: 20-February-17</i></p>
<p><b><a href="https://dineshonjava.com/2017/02/simpledateformat-in-java-format-date-to-string-to-date-with-example.html">SimpleDateFromat</a> Java Doc</b><br />
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.</p>
<p>SimpleDateFormat is an excellent utility for converting String to Date and then Date to String. It is not <a href="https://dineshonjava.com/2013/05/synchronization-in-java.html">thread-safe</a> so its better you create separate DateFormat for each <a href="https://dineshonjava.com/2013/04/creating-thread-in-java.html">thread</a> to avoid any race condition while parsing date in java.</p>
<p><b>There are some designator of SimpleDateFormat</b><br />
y ->; Year Year 1996; 96<br />
M ->; Month in year Month July; Jul; 07<br />
w ->; Week in year Number 27<br />
W ->; Week in month Number 2<br />
D ->; Day in year Number 189<br />
d ->; Day in month Number 10<br />
a ->; Am/pm marker Text PM<br />
H ->; Hour in day (0-23) Number 0<br />
h ->; Hour in am/pm (1-12) Number 12<br />
m ->; Minute in hour Number 30<br />
s ->; Second in minute Number 55<br />
S ->; Millisecond Number 978</p>
<p><b>Summary</b><br />
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 &#8220;m&#8221; and &#8220;M&#8221;, there are some letter are small case and same letter also available in capital case so they confused to developer some time.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><b>String Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/how-to-make-java-class-immutable/">How to make Immutable Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-interview-questions/">Core Java Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/string-class-in-java/">String Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/why-string-is-immutable-and-final-in-java/">Why String is Immutable and Final in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-string-to-integer-to-string-in-java-with-example/">Convert String to Integer to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-double-to-string-to-double-in-java-with-example/">Convert Double to String to Double in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-date-to-string-in-java-with-example/">Convert Date to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/differences-between-string-stringbuffer-and-stringbuilder-in-java/">Differences between String, StringBuffer and StringBuilder in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-stringbuilder-and-stringbuffer-in-java/">Differences StringBuffer and StringBuilder</a></b></li>
<li><b><a href="https://dineshonjava.com/how-to-compare-two-strings-in-java/">How to Compare two Strings in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/hashcode-and-equals-methods-in-java/">Difference between hashCode() and equals() methods in Java<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/java-string-substring-method/"> Java &#8211; String substring() Method<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-wait-and-sleep/"> Difference between wait() and sleep()<br />
</a></b></li>
</ol>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/convert-double-to-string-to-double-in-java-with-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/simpledateformat-in-java-format-date-to-string-to-date-with-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '66'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…