简体   繁体   中英

Java new Date with time stamp in date format

I have used the following

SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss zzz");  
Date date = new Date(); 
String formattedDate= df.format(date);

Date dateWithTime = df.parse(formattedDate);

i got the formatted date as string when i conver this into date i got error like

java.text.ParseException: Unparseable date: "Tue Feb 26 11:45:43 IST 2013"

How would convert to date or how i format a current date and get as date?

I think your code wouldn't throw ParseException. But it sure would definitely yield wrong output. your format should be:

"dd-MM-yyyy hh:mm:ss zzz"

Note that

  • MM---> Months
  • mm---> Minutes

Test with your code with out correcting the format:

Sat Jan 26 06:24:07 GMT 2013

Test with your code with correcting the format:

Tue Feb 26 06:20:51 GMT 2013

The date format should be as follows as shown in the exception. Change it to -

EEE MMM d hh:mm:ss z yyyy

正确的simpledateformat将是

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

请参考链接以获取正确的日期格式并解析SimpleDateFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM