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
You are correct in that
parseconverts a string to a date. Your issue that you’re taking the date and sending it toprintln, which essentially calls itstoString, 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 itsformatmethod: