1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Java Date to String Example This Java Date to String example shows how to convert java.util.Date to String in Java. */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class ConvertDateToStringExample { public static void main(String args[]){ //create new java.util.Date object Date date = new Date(); /* * To convert java.util.Date to String, use SimpleDateFormat class. */ /* * crate new SimpleDateFormat instance with desired date format. * We are going to use yyyy-mm-dd hh:mm:ss here. */ DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); //to convert Date to String, use format method of SimpleDateFormat class. String strDate = dateFormat.format(date); System.out.println("Date converted to String: " + strDate); } } /* Output of above given java.util.Date to String example would be Date converted to String: 2011-17-10 11:17:50 */ |
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDateToStringExample {
public static void main(String args[]){
//create new java.util.Date object
Date date = new Date();
/*
* To convert java.util.Date to String, use SimpleDateFormat class.
*/
/*
* crate new SimpleDateFormat instance with desired date format.
* We are going to use yyyy-mm-dd hh:mm:ss here.
*/
DateFormat dateFormat = new SimpleDateFormat(“yyyy-mm-dd hh:mm:ss”);
//to convert Date to String, use format method of SimpleDateFormat class.
String strDate = dateFormat.format(date);
System.out.println(“Date converted to String: ” + strDate);
}
}
/*
Output of above given java.util.Date to String example would be
Date converted to String: 2011-17-10 11:17:50
*/
you hav provided us a brilliant code but there is one mistake in that….u should define the format yyyy-MM-dd rather than yyyy-mm-dd.
you are right
thank you very much.