Date : Problem while parsing string to date

Hi All ,

I am trying to parse a string to a date object

publicstaticvoid main(String[] args)

{

try

{

DateFormat df =new SimpleDateFormat("dd-MMM-yyyy");

String tmpDate="27-Jul-2007";

Date date=df.parse(tmpDate);

System.out.println("Parsed date > "+ date);

}

catch(Exception e)

{

e.printStackTrace();

}

}

Following is the outcome :

Parsed date > Fri Jul 27 00:00:00 IST 2007

But for a fine reason , i want this parsed object in"dd-MMM-yyy"

format i.e. after parsing the string i want the date object to be in"dd-MMM-yyy"

format .

is there any way to parse a string to a perticular date format ?

thanks and regards

Mukul

[1258 byte] By [mukul.objecta] at [2007-11-27 11:56:10]
# 1

Eh, why are you stringifying the Date object if you want to have a formatted date String? After parsing, you have a long encapsuled in a Date instance, nothing else. Use sdf.format().

CeciNEstPasUnProgrammeura at 2007-7-29 19:06:05 > top of Java-index,Java Essentials,Java Programming...
# 2

> is there any way to parse a string to a perticular date format ?

You've already done that. What you want is for format the output to use that same pattern, like this:

public static void main(String[] args)

{

try

{

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");

String tmpDate="27-Jul-2007";

Date date=df.parse(tmpDate);

System.out.println("Parsed date > "+ df.format(date));

}

catch(Exception e)

{

e.printStackTrace();

}

}

%

duffymoa at 2007-7-29 19:06:05 > top of Java-index,Java Essentials,Java Programming...
# 3

I was bit confused

Thanks a lot

mukul.objecta at 2007-7-29 19:06:05 > top of Java-index,Java Essentials,Java Programming...