cancel
Showing results for 
Search instead for 
Did you mean: 

Date to String Conversion

Former Member
0 Kudos

Hi,

How to convert a java.util.date value to a string value?? Kindly help me with sample code.

Thanks,

Kalai.

Accepted Solutions (0)

Answers (1)

Answers (1)

roberto_tagliento
Active Contributor
0 Kudos

easiest:

String StrDate;

StrDate = DateVar.toString();

esaier:

String StrDate = new String("");

String DD = new String("");

String MM = new String("");

String YY = new String("");

DD = new Integer(StrDate.getDay()).toString();

MM = new Integer(StrDate.getMonth()).toString();

YY = new Integer(StrDate.getYear()).toString();

StrDate = DD + "-" + MM + "-" + YY;

exist also a specific CLASS, DateFormat

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html

examples:

http://javatechniques.com/public/java/docs/basics/dateformat-examples.html

*Please reward helpfull answer.

Message was edited by:

Roberto Tagliento

Former Member
0 Kudos

Hi,

Perhaps a more straightforward approach than the "easier" is to use the standard formatting introduced in JDK 1.5

String strDate = String.format("%1$td - %1$tm - %1$tY .", myDate);

here %1$tm - means take the first argument, treat it as a time and from that extract the month and put it in the string.

HTH

Peter

roberto_tagliento
Active Contributor
0 Kudos

Really nice!

Former Member
0 Kudos

SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy hh:mm:ss:SSS a");

String s = sdf.format(new Date());

and back:

Date d = sdf.parse(s);

-


for more details see help for SimpleDateFormat class