Converting Date to String in Java
- 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.
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
- How to make Immutable Class in Java
- Core Java Interview Questions
- String Class in Java
- Why String is Immutable and Final in Java
- Convert String to Integer to String in Java with Example
- Convert Double to String to Double in Java with Example
- Convert Date to String in Java with Example
- Differences between String, StringBuffer and StringBuilder in Java
- Differences StringBuffer and StringBuilder
- How to Compare two Strings in Java
- Difference between hashCode() and equals() methods in Java
- Java – String substring() Method
- Difference between wait() and sleep()