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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15082651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:25:35  来源:igfitidea点击:

Java new Date with time stamp in date format

java

提问by Hymanyesind

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?

如何转换为日期或我如何格式化当前日期并获得日期?

回答by PermGenError

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

我认为您的代码不会抛出 ParseException。但它肯定会产生错误的输出。你的格式应该是:

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

Note that

注意

  • MM---> Months
  • mm---> Minutes
  • MM---> 月
  • 毫米---> 分钟

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

回答by sagar

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

日期格式应如下所示,如异常所示。改成——

EEE MMM d hh:mm:ss z yyyy

回答by Travis G

The correct simpledateformat will be

正确的 simpledateformat 将是

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

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

回答by Mohan Raj B

Please refer the link for proper date formatting and parsing SimpleDateFormat

请参阅正确的日期格式和解析SimpleDateFormat的链接