Java 日期时间格式 AM/PM 配置
Java Date TIME Format AM/PM Configuration
我在格式化日期时遇到一些问题:
日期:2020 年 6 月 11 日04:14:20
日期格式:dd/MM/yyyy hh:mm:ss a
异常:
java.text.ParseException: Unparseable date: "11/06/2020 04:14:20"
代码如下
Blockquote
public String getFormatDate(String inputDate) {
String strDate = "";
try {
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss a");
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date1 = inputFormat.parse(inputDate);
strDate = outputFormat.format(date1);
}catch( Exception exe) {
exe.printStackTrace();
logger.error( "[ERROR] getFormatDate:. ", exe );
}
return strDate;
}
Blockquote
如有任何帮助,我们将不胜感激。
为您的日期和时间使用正确的日期时间对象
对于绝大多数用途,您不应将日期和时间保存在字符串中,也不应将日期和时间从一种格式的字符串转换为另一种格式的字符串。将日期和时间保存在 ZonedDateTime
或 LocalDateTime
对象中。
当您需要接受字符串输入时,立即将该输入解析为日期时间对象。我正在使用并推荐 java.time,现代 Java 日期和时间 API:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
String input = "11/06/2020 04:14:20";
LocalDateTime dateTime = LocalDateTime.parse(input, inputFormatter);
System.out.println(dateTime);
到目前为止的输出是:
2020-06-11T04:14:20
由于您的字符串中没有上午或下午,我假设 04:14:20 是 日 从 00:00:00 到 [=47] 的时间=].如果您另有打算,则需要说明具体方法。
仅当您需要给出字符串输出时,将您的日期和时间格式化回适当格式的字符串:
DateTimeFormatter outputFormatter = DateTimeFormatter
.ofPattern("MMMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
String output = dateTime.format(outputFormatter);
System.out.println(output);
June 11, 2020 04:14:20 AM
请为格式化程序提供语言环境,以便 Java 知道月份名称和 AM/PM 指示符使用哪种语言。
你的代码出了什么问题?
您的字符串没有上午或下午:11/06/2020 04:14:20
。然而,您的格式模式字符串最后需要一个 AM/PM 标记。这就是格式模式字母 a
所表示的。所以你的字符串不是你需要的格式。这就是您观察到的异常的原因。
Link
Oracle tutorial: Date Time 解释如何使用 java.time.
您可以检查此代码,您也必须使用日期字符串值传递 am/pm 部分,因为您的格式需要它。
//String date = "11/06/2020 04:14:20";
String date = "11/06/2020 04:14:20 am";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
感谢大家的帮助:
我已将源日期“11/06/2020 04:14:20”更改为“06/11/2020 04:14:20 PM”,然后在执行以下步骤后,它对我有用:
Blockquote
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
inputFormat.setTimeZone( TimeZone.getTimeZone("UTC") );
Date dDate = inputFormat.parse( srcDate );
String strDeDate = formatDateToString( dDate, "dd/MM/yyyy hh:mm:ss a", "IST" );
public String formatDateToString(Date date, String format,String timeZone) {
if (date == null) return null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
return sdf.format(date);
}
Blockquote
我在格式化日期时遇到一些问题:
日期:2020 年 6 月 11 日04:14:20
日期格式:dd/MM/yyyy hh:mm:ss a
异常:
java.text.ParseException: Unparseable date: "11/06/2020 04:14:20"
代码如下
Blockquote
public String getFormatDate(String inputDate) {
String strDate = "";
try {
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss a");
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date1 = inputFormat.parse(inputDate);
strDate = outputFormat.format(date1);
}catch( Exception exe) {
exe.printStackTrace();
logger.error( "[ERROR] getFormatDate:. ", exe );
}
return strDate;
}
Blockquote
如有任何帮助,我们将不胜感激。
为您的日期和时间使用正确的日期时间对象
对于绝大多数用途,您不应将日期和时间保存在字符串中,也不应将日期和时间从一种格式的字符串转换为另一种格式的字符串。将日期和时间保存在 ZonedDateTime
或 LocalDateTime
对象中。
当您需要接受字符串输入时,立即将该输入解析为日期时间对象。我正在使用并推荐 java.time,现代 Java 日期和时间 API:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
String input = "11/06/2020 04:14:20";
LocalDateTime dateTime = LocalDateTime.parse(input, inputFormatter);
System.out.println(dateTime);
到目前为止的输出是:
2020-06-11T04:14:20
由于您的字符串中没有上午或下午,我假设 04:14:20 是 日 从 00:00:00 到 [=47] 的时间=].如果您另有打算,则需要说明具体方法。
仅当您需要给出字符串输出时,将您的日期和时间格式化回适当格式的字符串:
DateTimeFormatter outputFormatter = DateTimeFormatter
.ofPattern("MMMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
String output = dateTime.format(outputFormatter);
System.out.println(output);
June 11, 2020 04:14:20 AM
请为格式化程序提供语言环境,以便 Java 知道月份名称和 AM/PM 指示符使用哪种语言。
你的代码出了什么问题?
您的字符串没有上午或下午:11/06/2020 04:14:20
。然而,您的格式模式字符串最后需要一个 AM/PM 标记。这就是格式模式字母 a
所表示的。所以你的字符串不是你需要的格式。这就是您观察到的异常的原因。
Link
Oracle tutorial: Date Time 解释如何使用 java.time.
您可以检查此代码,您也必须使用日期字符串值传递 am/pm 部分,因为您的格式需要它。
//String date = "11/06/2020 04:14:20";
String date = "11/06/2020 04:14:20 am";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
感谢大家的帮助:
我已将源日期“11/06/2020 04:14:20”更改为“06/11/2020 04:14:20 PM”,然后在执行以下步骤后,它对我有用:
Blockquote
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
inputFormat.setTimeZone( TimeZone.getTimeZone("UTC") );
Date dDate = inputFormat.parse( srcDate );
String strDeDate = formatDateToString( dDate, "dd/MM/yyyy hh:mm:ss a", "IST" );
public String formatDateToString(Date date, String format,String timeZone) {
if (date == null) return null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
return sdf.format(date);
}
Blockquote