How to change date format in a String in JAVA
The java.text.SimpleDateFormat class is used to both parse and format dates according to a formatting pattern you specify yourself. When parsing dates, the Java SimpleDateFormat typically parses the date from a Java String. When formatting dates, the SimpleDateFormat typically formats a Date object into a String, although it can also format the date into a StringBuffer.
This text explains how to use the SimpleDateFormat class to format dates.
Creating a SimpleDateFormat
You create a SimpleDateFormat instance like this:
String pattern = “yyyy-MM-dd”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
The pattern parameter passed to the SimpleDateFormat constructor is the pattern to use for parsing and formatting of dates. The pattern syntax is covered later in this text. The pattern is just a regular Java String.
Formatting Dates
Once you have created a SimpleDateFormat instance you can format dates using its format() method. Here is an example:
String pattern = “yyyy-MM-dd”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
The Date instance passed to the format() method is a java.util.Date instance.
The output printed from the above SimpleDateFormat example would be:
2018-09-09
Notice how the formatted date string starts with the year, then month, then day. The sequence of the date fields are determined by the date pattern passed to the SimpleDateFormat constructor. As mentioned earlier, this format will be explained a bit later in this Java SimpleDateFormat tutorial.
Format Date Into StringBuffer
The Java SimpleDateFormat class is also capable of formatting a Date object into a StringBuffer, instead of returning an individual String with the date formatted. The SimpleDateFormat class does this via a version of the format() method that takes the Date, StringBuffer and a FieldPosition instance as parameters.
Here is an example of formatting a date into a StringBuffer using Java SimpleDateFormat :
StringBuffer stringBuffer = new StringBuffer();
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ssZ”);
simpleDateFormat.format(now, stringBuffer, new FieldPosition(0));
It is not exactly clear how the FieldPosition instance is used. It seems the format() method appends the formatted String to the end of the StringBuffer no matter what the int value passed to the FieldPosition constructor is.
Parsing Dates
You can parse a String into a java.util.Date instance using the parse() method of the SimpleDateFormat instance. Here is an example:
String pattern = “yyyy-MM-dd”;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse(“2018-09-09”);
Once this code is executed, the date variable points to a Date instance representing september 9th, 2018.
Creating a SimpleDateFormat For a Specific Locale
You can create a SimpleDateFormat instance targeted at a specific Java Locale. Doing so will format the dates according to that Locale whenever relevant. For instance, a formatting pattern including the name of the week day will write the week day in the language of the given Locale. Here is an example:
String pattern = “EEEEE dd MMMMM yyyy HH:mm:ss.SSSZ”;
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat(pattern, new Locale(“da”, “DK”));
String date = simpleDateFormat.format(new Date());
System.out.println(date);
Pattern Examples
Here are a few Java SimpleDateFormat date pattern examples:
Pattern Example
dd-MM-yy 31-01-12
dd-MM-yyyy 31-01-2012
MM-dd-yyyy 01-31-2012
yyyy-MM-dd 2012-01-31
yyyy-MM-dd HH:mm:ss 2012-01-31 23:59:59
yyyy-MM-dd HH:mm:ss.SSS 2012-01-31 23:59:59.999
yyyy-MM-dd HH:mm:ss.SSSZ 2012-01-31 23:59:59.999+0100
EEEEE MMMMM yyyy HH:mm:ss.SSSZ Saturday November 2012 10:45:42.720+0100
DateTimeFormatter
We can convert Date to String in java using format() method of java.text.DateFormat class.
The format() method of DateFormat class is used to convert Date into String. DateFormat is an abstract class. The child class of DateFormat is SimpleDateFormat. It is the implementation of DateFormat class.
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat(“yyyy-mm-dd hh:mm:ss”);
String strDate = dateFormat.format(date);Also Read : What is Custom Exception in JAVA ?
package com.mycodetips;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormatExample1 {
// date format 1
private static final DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
// date format 2
private static final DateTimeFormatter dateFormatterNew
= DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a");
public static void main(String[] args) {
String date = "2019-05-23 00:00:00.0";
// string to LocalDateTime
LocalDateTime ldateTime = LocalDateTime.parse(date, dateFormatter);
System.out.println(dateFormatter.format(ldateTime));
// change date format
System.out.println(dateFormatterNew.format(ldateTime));
}
}
Output
2019-05-23 00:00:00.0
Thursday, May 23, 2019 00:00:00 AM
SimpleDateFormat
Convert the String to Date and change the date format with SimpleDateFormat
package com.mycodetips;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
private static final SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
private static final SimpleDateFormat sdfNew =
new SimpleDateFormat("EEEE, MMM d, yyyy HH:mm:ss a");
public static void main(String[] args) {
String dateString = "2019-05-23 00:00:00.0";
try {
// string to date
Date date = sdf.parse(dateString);
System.out.println(sdf.format(date));
System.out.println(sdfNew.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output
2019-05-23 00:00:00.0
Thursday, May 23, 2019 00:00:00 AM
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.