September 21, 2022

Convert Date to String in Java

In this post we’ll see how to convert Date to String in Java.

For converting String to Date in Java check this post- Convert String to Date in Java

Before Java 8, SimpleDateFormat was the class to use for converting Date to String with the specified formatting. Java 8 onward there is another option java.time.format.DateTimeFormatter class that can be used for the conversion.

Converting Date to String using SimpleDateFormat

While creating an instance of SimpleDateFormat you can pass the pattern for formatting. SimpleDateFormat has a format method which takes Date instance as parameter and returns the formatted date (and time) string.

Here is an example where current date is converted to String using different date and time formatting patterns.

import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatDate {
  public static void main(String[] args) {
    // For date in format 2019.07.04 AD at 11:08:54 IST
    formatDate("yyyy.MM.dd G 'at' HH:mm:ss z");
    // For date in format Mon, Oct 7, '19
    formatDate("EEE, MMM d, ''yy");
    // For date in format Monday, October 07, 2019
    formatDate("EEEE, MMMM dd, yyyy");
    // For time in format 07 o'clock PM, India Standard Time
    formatDate("hh 'o''clock' a, zzzz");
    // For time in 24 Hr format 19:41:59:635 PM
    formatDate("HH:mm:ss:SSS a");
    // For date-time in format 2019-10-07T19:27:38.571+0530
    formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
    // For date in format 05/08/2016
    formatDate("MM/dd/yyyy");
    // For date in format 07/10/2019 19:29:40 PM
    formatDate("dd/MM/yyyy HH:mm:ss a");
    // For date in format 07/10/2019 19:29:40 PM
    formatDate("dd/MMM/yyyy GGG HH:mm:ss:SSS a");
  }

  private static void formatDate(String pattern){
    Date dt = new Date();
    // Create date format as per specified pattern
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String formattedDate = sdf.format(dt);
    System.out.println("Formatted Date- " + formattedDate +
              " for Pattern: " + pattern); 
  }
}
Output
Formatted Date- 2019.10.09 AD at 18:15:53 IST for Pattern: yyyy.MM.dd G 'at' HH:mm:ss z
Formatted Date- Wed, Oct 9, '19 for Pattern: EEE, MMM d, ''yy
Formatted Date- Wednesday, October 09, 2019 for Pattern: EEEE, MMMM dd, yyyy
Formatted Date- 06 o'clock PM, India Standard Time for Pattern: hh 'o''clock' a, zzzz
Formatted Date- 18:15:53:978 PM for Pattern: HH:mm:ss:SSS a
Formatted Date- 2019-10-09T18:15:53.979 +0530 for Pattern: yyyy-MM-dd'T'HH:mm:ss.SSS Z
Formatted Date- 10/09/2019 for Pattern: MM/dd/yyyy
Formatted Date- 09/10/2019 18:15:53 PM for Pattern: dd/MM/yyyy HH:mm:ss a
Formatted Date- 09/Oct/2019 AD 18:15:53:981 PM for Pattern: dd/MMM/yyyy GGG HH:mm:ss:SSS a

Converting Date to String using DateTimeFormatter

In DateTimeFormatter class there is a static method ofPattern() using which you can specify the pattern for date time formatting.

using format() method of the LocalDate (representing date), LocalTime (representing time) and LocalDateTime (representing date and time) you can convert Date to String.

DateTimeFormatter instance created using ofPattern() method is passed as parameter in format() method.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class FormatDate {
  public static void main(String[] args) {
    // LocalDateTime
    // For date in format 2019.07.04 AD at 11:08:54 IST
    LocalDateTime dateTime = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd G 'at' HH:mm:ss");
    String formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
    
    // For date in format Mon, Oct 7, '19
    formatter = DateTimeFormatter.ofPattern("EEE, MMM d, ''yy");
    formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);

    // For date in format Monday, October 07, 2019
    formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy");
    formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
    
    // For date-time in format 2019-10-07T19:27:38.571+0530
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
    formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
    
    // For date in format 07/10/2019 19:29:40 PM
    formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss a");
    formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
    
    // For date in format 07/Oct/2019 AD 14:25:51:048 PM
    formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy GGG HH:mm:ss:SSS a");
    formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
    
    // LocalTime
    LocalTime time = LocalTime.now();
    // For time in 24 Hr format 19:41:59:635 PM
    formatter = DateTimeFormatter.ofPattern("HH:mm:ss:SSS a");
    formattedDate = time.format(formatter);
    System.out.println("Formatted Time- " + formattedDate);
    
    // LocalDate
    LocalDate date = LocalDate.now();
    // For date in format 05/08/2016
    formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    formattedDate = date.format(formatter);
    System.out.println("Formatted Date- " + formattedDate);
  }
	
}
Output
Formatted Date- 2019.10.10 AD at 14:27:38
Formatted Date- Thu, Oct 10, '19
Formatted Date- Thursday, October 10, 2019
Formatted Date- 2019-10-10T14:27:38.014
Formatted Date- 10/10/2019 14:27:38 PM
Formatted Date- 10/Oct/2019 AD 14:27:38:014 PM
Formatted Time- 14:27:38:194 PM
Formatted Date- 10/10/2019

If you have Zone offset (Z) or time zone name (z) in the patterns then you’d need a ZonedDateTime instance as LocalDateTime does not have a field or value for the timezone.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class FormatDate {

  public static void main(String[] args) {
    formatDate("yyyy.MM.dd G 'at' HH:mm:ss z");
    // For time in format 07 o'clock PM, India Standard Time
    formatDate("hh 'o''clock' a, zzzz");
    // For date-time in format 2019-10-07T19:27:38.571+0530
    formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
  }

  private static void formatDate(String pattern){
    ZonedDateTime dateTime = ZonedDateTime.now();
    // Create DateTimeFormatter instance as per specified pattern
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    String formattedDate = dateTime.format(formatter);
    System.out.println("Formatted Date- " + formattedDate +
              " for Pattern: " + pattern); 
  }
}
Output
Formatted Date- 2019.10.09 AD at 18:25:00 IST for Pattern: yyyy.MM.dd G 'at' HH:mm:ss z
Formatted Date- 06 o'clock PM, India Standard Time for Pattern: hh 'o''clock' a, zzzz
Formatted Date- 2019-10-09T18:25:00.975 +0530 for Pattern: yyyy-MM-dd'T'HH:mm:ss.SSS Z

Using DateTimeFormatter.ofLocalizedDate() method

In DateTimeFormatter class there are also following static methods that can be used for converting date and time to String.

  • ofLocalizedDate(FormatStyle dateStyle)- Returns a locale specific date format for the ISO chronology.
  • ofLocalizedDateTime(FormatStyle dateTimeStyle)- Returns a locale specific date-time formatter for the ISO chronology.
  • ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle)- Returns a locale specific date and time format for the ISO chronology.
  • ofLocalizedTime(FormatStyle timeStyle)- Returns a locale specific time format for the ISO chronology.

Here java.time.format.FormatStyle is an Enum that has the following constant fields-

  • FULL- Full text style, with the most detail. For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
  • LONG- Long text style, with lots of detail. For example, the format might be 'January 12, 1952'.
  • MEDIUM- Medium text style, with some detail. For example, the format might be 'Jan 12, 1952'.
  • SHORT- Short text style, typically numeric. For example, the format might be '12.13.52' or '3:30pm'.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class FormatDate {
  public static void main(String[] args) {
    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println("Full format- " +dateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));
    System.out.println("LONG format- " +dateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)));
    System.out.println("MEDIUM format- " +dateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)));
    System.out.println("SHORT format- " +dateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));
  }
}
Output
Full format- Wednesday, 9 October, 2019
LONG format- 9 October 2019
MEDIUM format- 09-Oct-2019
SHORT format- 09/10/19

That's all for the topic Convert Date to String in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment