Popular Tutorials
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.
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…