nath
nath

Reputation: 2848

Convert string to date

I have string of format 01-Jan-11 and I need to parse it to a Date in the format of 01-Jan-11. Problem is when I try to do that always I got out put some thing like below. Mon Jan 01 00:00:00 GMT+05:30 2011

Can some one pls help me to do that?

try {

 String str_date="11-Jan-11";
 DateFormat formatter ; 
 Date date ; 
      formatter = new SimpleDateFormat("dd-MMM-yy");
          date = (Date)formatter.parse(str_date);    
           System.out.println("Today is " +date);
} catch (ParseException e)

{System.out.println("Exception :"+e);    }    
     }

Time Zone is GMT+05:30 Kalkata

Upvotes: 0

Views: 1533

Answers (3)

Steve Kuo
Steve Kuo

Reputation: 63134

You are correct in that parse converts a string to a date. Your issue that you're taking the date and sending it to println, which essentially calls its toString, which has all the other stuff that you don't want (seconds, GMT offset, etc). Since you already have a formatter configured to your needs, simply use its format method:

System.out.println("Today is " + formatter.format(date));

Upvotes: 2

Eric Giguere
Eric Giguere

Reputation: 3505

The SimpleDateFormat class is what you want, check out this example here:

http://www.roseindia.net/java/beginners/CalendarExample.shtml

Upvotes: 2

NG.
NG.

Reputation: 22914

Check out SimpleDateFormat.

Upvotes: 2

Related Questions