<div dir="ltr" style="text-align: justify;"><strong>SimpleDateFormat</strong> is an excellent utility class for formatting and parsing dates in a re-presentable manner. It allows for formatting Date to String and parsing String to Date. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. 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.SimpleDateFormat class has two methods format() method to format Date to String and parse() method parse String to Date and time in java. It is a concrete class for formatting and parsing date which inherits java.text.DateFormat class.</p>
<ul style="text-align: left;">
<li><b>format() </b>formatting method means converting date to string</li>
<li><b>parse() </b>parsing method means converting string to date</li>
</ul>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img title="SimpleDateFormat in Java" src="https://dineshonjava.com/wp-content/uploads/2017/02/Convert-Date-to-String.png" border="0" /></div>
<p> ;</p>
<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>There various <b>attributes available in SimpleDateFormat </b>class in java for pattern, As per Java Doc following:</p>
<table border="1" align="center">
<tbody>
<tr>
<th scope="col">Letter</th>
<th scope="col">Date or Time Component</th>
<th scope="col">Presentation</th>
<th scope="col">Examples</th>
</tr>
<tr>
<td>G</td>
<td>Era designator</td>
<td>Text</td>
<td>AD</td>
</tr>
<tr>
<td>y</td>
<td>Year</td>
<td>Year</td>
<td>1996; 96</td>
</tr>
<tr>
<td>M</td>
<td>Month in year</td>
<td>Month</td>
<td>July; Jul; 07</td>
</tr>
<tr>
<td>w</td>
<td>Week in year</td>
<td>Number</td>
<td>27</td>
</tr>
<tr>
<td>W</td>
<td>Week in month</td>
<td>Number</td>
<td>2</td>
</tr>
<tr>
<td>D</td>
<td>Day in year</td>
<td>Number</td>
<td>189</td>
</tr>
<tr>
<td>d</td>
<td>Day in month</td>
<td>Number</td>
<td>10</td>
</tr>
<tr>
<td>F</td>
<td>Day of week in month</td>
<td>Number</td>
<td>2</td>
</tr>
<tr>
<td>E</td>
<td>Day in week</td>
<td>Text</td>
<td>Tuesday; Tue</td>
</tr>
<tr>
<td>a</td>
<td>Am/pm marker</td>
<td>Text</td>
<td>PM</td>
</tr>
<tr>
<td>H</td>
<td>Hour in day (0-23)</td>
<td>Number</td>
<td>0</td>
</tr>
<tr>
<td>k</td>
<td>Hour in day (1-24)</td>
<td>Number</td>
<td>24</td>
</tr>
<tr>
<td>K</td>
<td>Hour in am/pm (0-11)</td>
<td>Number</td>
<td>0</td>
</tr>
<tr>
<td>h</td>
<td>Hour in am/pm (1-12)</td>
<td>Number</td>
<td>12</td>
</tr>
<tr>
<td>m</td>
<td>Minute in hour</td>
<td>Number</td>
<td>30</td>
</tr>
<tr>
<td>s</td>
<td>Second in minute</td>
<td>Number</td>
<td>55</td>
</tr>
<tr>
<td>S</td>
<td>Millisecond</td>
<td>Number</td>
<td>978</td>
</tr>
<tr>
<td>z</td>
<td>Time zone</td>
<td>General time zone</td>
<td>Pacific Standard Time; PST; GMT-08:00</td>
</tr>
<tr>
<td>Z</td>
<td>Time zone</td>
<td>RFC 822 time zone</td>
<td>-0800</td>
</tr>
</tbody>
</table>
<h2><b><br />
</b> <b>SimpleDateFormat Example in Java</b></h2>
<h3><b>Format Date to String</b></h3>
<p>In this example we are going to format dates using SimpleDateFormat. We can format Date to String to our defined format e.g &#8220;dd-MM-yyyy&#8221;. This format will print dates in that format e.g. 20-02-2017. We can define any format by using letter patterns supported by SimpleDateFormat in java e.g. d means day of month, y means year and M means Month of year as mention in above list in this tutorial. There following steps to <a href="https://dineshonjava.com/2017/02/convert-date-to-string-in-java-with-example.html">convert date to string</a>:</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>
<p>Let&#8217;s see the simple example to format date in java using java.text.SimpleDateFormat class.</p>
<pre class="highlight">import java.text.SimpleDateFormat; 
import java.util.Date; 
 
/** 
 * 
 */ 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SimpleDateFormatExample { 
 
 /** 
 * @param args 
 */ 
 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); 
 
 } 
 
} 
 
</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></p>
<h3><b>Parse String to Date</b></h3>
<p>java.text.SimpleDateFormat class is also used to format Date to String as date and time. SimpleDateFormat formats date in same pattern whatever we are provided to it while creating instance of SimpleDateFormat. So we can also include time information like hour, minutes and seconds while formatting dates.</p>
<p>Let&#8217;s see the full example to format date and time in java using java.text.SimpleDateFormat class.</p>
<pre class="highlight">import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
/** 
 * 
 */ 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SimpleDateFormatExample { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 
 //parse string to date dd/MM/yyyy format e.g. "20-02-2017" 
 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
 try { 
 Date date = formatter.parse("20/02/2017"); 
 System.out.println("Date is: "+date); 
 } catch (ParseException e) { 
 e.printStackTrace(); 
 } 
 
 //parse string to date dd-MM-yyyy format e.g. "20-02-2017" 
 formatter = new SimpleDateFormat("dd-MM-yyyy"); 
 try { 
 Date date = formatter.parse("20-02-2017"); 
 System.out.println("Date is: "+date); 
 } catch (ParseException e) { 
 e.printStackTrace(); 
 } 
 
 } 
 
} 
 
</pre>
<p><b>Output:</b><br />
<i>Date is: Mon Feb 20 00:00:00 IST 2017</i><br />
<i>Date is: Mon Feb 20 00:00:00 IST 2017</i></p>
<p><b>Summary </b><br />
In this tutorial we have seen how to format date to string and parse string to date using SimpleDateFormat with example. There are two methods exists in this concrete class format() and parse(). format() used for formatting means converting date to string and parse() used for parsing means converting string to date.</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>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/convert-date-to-string-in-java-with-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/best-practices-for-java-naming-convention-in-programming/">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: '65'});
});
</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…