SimpleDateFormat 中的月份问题 class
Month issue in SimpleDateFormat class
String expiryDate = "2016-03-14";
private String formatDateTime(String expiryDate, String expiryTime) {
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat output = new SimpleDateFormat("MMM, dd, yyyy");
try {
String formattedDate = output.format(input.parse(expiryDate ));// parse input
return formattedDate + ", " + expiryTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
然而,当它 returns 时,它给出 2016 年 1 月 14 日。
期望的输出是 2016 年 3 月 14 日
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
应该是
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
mm应该是MM
mm代表分钟,所以当你使用mm时,它会打印分钟而不是月。
而MM代表月份。您需要这样添加:
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
mm
应该是大写 MM
。因为
mm表示分
MM代表月份
所以你的格式应该是这样的
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
以上语句需要更正如下:
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
使用 MM 而不是 mm,因为 MM 用于月份而 mm 用于分钟。
java.text.SimpleDateFormat
内部调用 java.util.Locale
,它实际实现了日期时间和区域格式模式。因此,In Locale
MM 定义为月份,mm 定义为分钟。所以,使用
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
不会匹配任何模式,因此默认情况下月份将设置为其默认值 1
。它显示为 Jan
而不是 Mar
(您实际上期望的)。
使用MM代表月份。它会解决你的问题。
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
您的问题已得到解答,其他答案均正确。我想提供您方法的现代版本。
private static DateTimeFormatter output
= DateTimeFormatter.ofPattern("MMM, dd, yyyy", Locale.ENGLISH);
private static String formatDateTime(String expiryDate, String expiryTime) {
String formattedDate = LocalDate.parse(expiryDate).format(output);
return formattedDate + ", " + expiryTime;
}
当我使用 2016-03-14
的 expiryDate
调用此方法时,我按预期返回了 Mar, 14, 2016
。
java.time
您正在使用的 SimpleDateFormat
(其他答案也使用)不仅早已过时,而且出了名的麻烦。我建议您忘记它,而是使用 java.time
,现代的 Java 日期和时间 API。与它一起工作要好得多。
您的输入格式 2016-03-14
符合 ISO 8601,即现代 类 解析为默认格式的格式,即没有任何明确的格式化程序。因此,甚至没有机会在格式模式字符串中弄错 MM
的情况。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.
String expiryDate = "2016-03-14";
private String formatDateTime(String expiryDate, String expiryTime) {
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat output = new SimpleDateFormat("MMM, dd, yyyy");
try {
String formattedDate = output.format(input.parse(expiryDate ));// parse input
return formattedDate + ", " + expiryTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
然而,当它 returns 时,它给出 2016 年 1 月 14 日。
期望的输出是 2016 年 3 月 14 日
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
应该是
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
mm应该是MM
mm代表分钟,所以当你使用mm时,它会打印分钟而不是月。
而MM代表月份。您需要这样添加:
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
mm
应该是大写 MM
。因为
mm表示分 MM代表月份
所以你的格式应该是这样的
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
以上语句需要更正如下:
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
使用 MM 而不是 mm,因为 MM 用于月份而 mm 用于分钟。
java.text.SimpleDateFormat
内部调用 java.util.Locale
,它实际实现了日期时间和区域格式模式。因此,In Locale
MM 定义为月份,mm 定义为分钟。所以,使用
SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
不会匹配任何模式,因此默认情况下月份将设置为其默认值 1
。它显示为 Jan
而不是 Mar
(您实际上期望的)。
使用MM代表月份。它会解决你的问题。
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
您的问题已得到解答,其他答案均正确。我想提供您方法的现代版本。
private static DateTimeFormatter output
= DateTimeFormatter.ofPattern("MMM, dd, yyyy", Locale.ENGLISH);
private static String formatDateTime(String expiryDate, String expiryTime) {
String formattedDate = LocalDate.parse(expiryDate).format(output);
return formattedDate + ", " + expiryTime;
}
当我使用 2016-03-14
的 expiryDate
调用此方法时,我按预期返回了 Mar, 14, 2016
。
java.time
您正在使用的 SimpleDateFormat
(其他答案也使用)不仅早已过时,而且出了名的麻烦。我建议您忘记它,而是使用 java.time
,现代的 Java 日期和时间 API。与它一起工作要好得多。
您的输入格式 2016-03-14
符合 ISO 8601,即现代 类 解析为默认格式的格式,即没有任何明确的格式化程序。因此,甚至没有机会在格式模式字符串中弄错 MM
的情况。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.