Java日期时间格式AMPM配置

问题描述 投票:-3回答:1

我在格式化日期的时候遇到了一些问题。

日期:11062020 04:14:20

日期格式: 日期:11062020 04:14:20ddMMyyyy hh:mm:ss a。

异常。

java.text.ParseException: Unparseable date: "11062020 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

Any help would be greatly appeciated.

java simpledateformat
1个回答
1
投票

你可以检查这段代码,你必须把ampm部分也和日期字符串值一起传递,因为你的格式正在期待这个。

//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");

https:/ideone.com3nibwJ


1
投票

为您的日期和时间使用正确的日期-时间对象。

在绝大多数情况下,你不应该将日期和时间保存在一个字符串中,也不应该将日期和时间从一种格式的字符串转换为另一种格式的字符串。将日期和时间保存在 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到23:59:59。如果你不是这样打算的,你需要解释如何。

只有当你需要给出字符串输出时,才将日期和时间格式化,回馈为适当格式的字符串。

    DateTimeFormatter outputFormatter = DateTimeFormatter
            .ofPattern("MMMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
    String output = dateTime.format(outputFormatter);
    System.out.println(output);

2020年6月11日 04: 14: 20 AM。

请为formatter提供一个locale,这样Java就知道月名和AMPM指标应该使用哪种语言。

你的代码出了什么问题?

你的字符串没有AM也没有PM。11/06/2020 04:14:20. 然而你的格式模式字符串却需要在最后加上AMPM标记。这就是格式模式信 a 表示。所以你的字符串不是你要求的格式。这就是你观察到的异常的原因。

链接

甲骨文教程。日期时间 解释如何使用java.time.com。


1
投票

谢谢大家的帮助。

我把源日期 "11062020 04:14:20 "改成了 "06112020 04:14:20 PM",然后在执行了下面的步骤后,它为我工作了。

阻止引用

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);
}

句号

© www.soinside.com 2019 - 2024. All rights reserved.