Popular Tutorials
There various attributes available in SimpleDateFormat class in java for pattern, As per Java Doc following:
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
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 |
F | Day of week in month | Number | 2 |
E | Day in week | Text | Tuesday; Tue |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | 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 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
In this example we are going to format dates using SimpleDateFormat. We can format Date to String to our defined format e.g “dd-MM-yyyy”. 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 convert date to string:
Let’s see the simple example to format date in java using java.text.SimpleDateFormat class.
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); } }
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
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.
Let’s see the full example to format date and time in java using java.text.SimpleDateFormat class.
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(); } } }
Output:
Date is: Mon Feb 20 00:00:00 IST 2017
Date is: Mon Feb 20 00:00:00 IST 2017
Summary
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.
String Related Posts
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…