In this article, we will learn how to convert Date to String in Java using different date formats
Q) What is the need of converting Date to String ?
- Generally, whenever we receive or send data from web application then it all passed in the form of String only
- For displaying or transfer over the wire or network, Date has to be converted into String
- This article explains about Date to String conversion in different formats
- Note: Likewise, sometimes String to Date conversion is also required for reverse process
1. Date to String conversion in different formats :
We will use below formats to convert Date to String
- dd/MM/yyyy
- dd-MM-yyyy
- dd.MM.yyyy
- dd MM yyyy
- MM dd, yyyy
- dd-MMM-yyyy
- E, MMM dd yyyy
- E, MMM dd yyyy HH:mm:ss
- E, MMM dd yyyy hh:mm:ss a
Note: for creating above formats we need mainly 2 classes
2. Date to String conversion examples :
2.1 Convert Date to String in (dd/MM/yyyy) format :
DateToStringConversion1.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion1 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:23:58 IST 2022
After formatting Date to String :- 20/04/2022
2.2 Convert Date to String in (dd-MM-yyyy) format :
DateToStringConversion2.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion2 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:24:20 IST 2022
After formatting Date to String :- 20-04-2022
2.3 Convert Date to String in (dd MM yyyy) format :
DateToStringConversion3.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion3 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("dd MM yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:24:40 IST 2022
After formatting Date to String :- 20 04 2022
2.4 Convert Date to String in (dd.MM.yyyy) format :
DateToStringConversion4.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion4 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:25:01 IST 2022
After formatting Date to String :- 20.04.2022
2.5 Convert Date to String in (MM dd, yyyy) format :
DateToStringConversion5.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion5 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("MM dd, yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:25:28 IST 2022
After formatting Date to String :- 04 20, 2022
2.6 Convert Date to String in (dd-MMM-yyyy) format :
DateToStringConversion6.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion6 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:25:47 IST 2022
After formatting Date to String :- 20-Apr-2022
2.7 Convert Date to String in (E, MMM dd yyyy) format :
DateToStringConversion7.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion7 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:26:09 IST 2022
After formatting Date to String :- Wed, Apr 20 2022
2.8 Convert Date to String in (E, MMM dd yyyy HH:mm:ss) format :
DateToStringConversion8.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion8 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:26:31 IST 2022
After formatting Date to String :- Wed, Apr 20 2022 15:26:31
2.9 Convert Date to String in (E, MMM dd yyyy hh:mm:ss a) format :
DateToStringConversion9.java
package in.bench.resources.date.to.string.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion9 {
public static void main(String[] args) throws ParseException {
// get current date
Date date = new Date();
// formatter
DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy hh:mm:ss a");
// format date using above formatter to convert into String
String strDate = formatter.format(date);
// print to console
System.out.println("Original Date :- " + date);
System.out.println("\nAfter formatting Date to String :- " + strDate);
}
}
Output:
Original Date :- Wed Apr 20 15:26:50 IST 2022
After formatting Date to String :- Wed, Apr 20 2022 03:26:50 PM
Hope, everyone found this article useful while converting Date to String. If any edge cases needs to be covered then comment
Related Articles:
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Happy Coding !!
Happy Learning !!