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 开发者_StackOverflow中文版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 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));
Check out SimpleDateFormat.
The SimpleDateFormat class is what you want, check out this example here:
http://www.roseindia.net/java/beginners/CalendarExample.shtml
精彩评论