java tutorial - How to Convert Date to String in Java - java programming - learn java - java basics - java for beginners



 convert date to string

Learn Java - Java tutorial - convert date to string - Java examples - Java programs

Convert Date to String in Java

  • 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.
  • The function converts a Date to a String.
  • In the below function I have used the format yyyy-MM-dd hh:mm:ss, however if you want the result in any other format then you can simply modify the pattern in SimpleDateFormat.

sample code:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDateToString {
    public static void main(String args[]) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strDate = dateFormat.format(date);
        System.out.println("converted Date to String: " + strDate);
    }
}

Output:

converted Date to String: 2016-59-17 08:59:49

Related Searches to How to Convert Date to String in Java