Java - 日期构造函数接受日期字符串,但不推荐使用。尝试了替代方案,但没有运气

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6876155/
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-30 17:42:08  来源:igfitidea点击:

Java - Date constructor accepts a Date String, But deprecated. Tried alternatives, but no luck

javadatetimedate

提问by pingu

String temp_date="07/28/2011 11:06:37 AM";  
Date date = new Date(temp_date); //Depricated 
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss"); 
String comp_date= sdf.format(date);
System.out.println(comp_date);

This works, But If I use something like this

这有效,但如果我使用这样的东西

String temp_date="07/28/2011 11:06:37 AM";  
try{  
    SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss"); 
    Date comp_date= sdf.parse(temp_date);
    System.out.println(comp_date);
}catch(Exception e){
    System.out.println(e);
}

This exception is thrown:

抛出此异常:

java.text.ParseException: Unparseable date: "07/28/2011 11:06:37 AM"

回答by BalusC

Your parsing pattern is wrong. It does not match the date string representation. The MMMdenotes a 3-letter localized month abbreviation, while you have 2-digit month number in your actual date, you need MM. You've also slashes /as date/month/year separator and not -. For the AM/PM marker you also need an aafterwards so that the right hhcan be parsed.

你的解析模式是错误的。它与日期字符串表示形式不匹配。该MMM表示3个字母的本地化月份缩写,而你在你的实际日期2位数的月份数,你需要MM。您还将斜线/作为日期/月/年分隔符而不是-. 对于 AM/PM 标记,您还需要一个aafter 以便hh可以解析正确的。

This should work:

这应该有效:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); 

For an explanation of those patterns, read the SimpleDateFormatjavadoc.

有关这些模式的解释,请阅读SimpleDateFormatjavadoc



I believe that your concrete functional requirement is to convert the given date string as specified by the pattern MM/dd/yyyy hh:mm:ss ainto another date string format, as specified by the pattern MMM-dd-yyyy hh:mm:ss. In that case, you should then have two SimpleDateFormatinstances, one which parses the string in the given pattern to a Dateand another which formats the parsed Dateto the given pattern. This should do what you want:

我相信您的具体功能要求是将模式指定的给定日期字符串转换为模式指定的MM/dd/yyyy hh:mm:ss a另一种日期字符串格式MMM-dd-yyyy hh:mm:ss。在这种情况下,您应该有两个SimpleDateFormat实例,一个将给定模式中的字符串解析为 a Date,另一个将解析后的字符串格式化Date为给定模式。这应该做你想做的:

String inputDate = "07/28/2011 11:06:37 AM";
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(inputDate);
String outputDate = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").format(date);
System.out.println(outputDate); // Jul-28-2011 11:06:37

Note that I changed hhin output to be HHbecause it would otherwise end up in 1-12 hour representation withoutan AM/PM marker. The HHrepresents it as 0-23 hour.

请注意,我将hh输出更改为是HH因为否则它会以 1-12 小时的表示结束而没有AM/PM 标记。将其HH表示为 0-23 小时。

回答by pingu

The format you gave the SimpleDateFormat uses -between the month, date, and year. Your string uses slashes.

您为 SimpleDateFormat 提供的格式-在月、日和年之间使用。您的字符串使用斜杠。

回答by Jeffrey Kevin Pry

At first look, it looks as if your format string is wrong.

乍一看,好像您的格式字符串是错误的。

MMM -- You specified this for the month, but you aren't passing a 3 char month.

MMM——你指定了这个月,但你没有通过 3 个字符的月份。

Try MM and see if that helps.

试试MM,看看是否有帮助。

Take a look at this for some additional date format information:

看看这个一些额外的日期格式信息:

http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm

http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm