Convert Date to String - Java Date Time

Java examples for Date Time:Legacy Date

Introduction

To convert java.util.Date to String, use SimpleDateFormat class.

create new SimpleDateFormat instance with desired date format.

We are going to use yyyy-mm-dd hh:mm:ss here.

Demo Code


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
 
        public static void main(String args[]){
                Date date = new Date();
                DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
               /*from  w  w w .  ja v a  2s. c om*/
                String strDate = dateFormat.format(date);
               
                System.out.println("Date converted to String: " + strDate);
               
        }
}
 

Result


Related Tutorials