简体   繁体   English

Java新日期,带有日期格式的时间戳

[英]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. 我认为您的代码不会抛出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 --->月
  • 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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