当前位置: 首页 > 知识库问答 >
问题:

java.text.ParseException:无法解析的日期:java.text.DateFormat.Parse(DateFormat.java:579)

左丘阳晖
2023-03-14
SimpleDateFormat dtfmt=new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.getDefault());
Date dt=dtfmt.parse(deptdt);

w/system.err:java.text.ParseException:不可解析日期:“24 Oct 2016 7:31 PM”(偏移量3)w/system.err:java.text.DateFormat.Parse(DateFormat.java:579)

有解决办法吗?

共有1个答案

卢作人
2023-03-14

由于给定的日期时间是英文的,因此应该在日期时间解析器中使用locale.English;否则,解析将在使用非英语类型区域设置的系统(计算机、电话等)中失败。

另外,请注意,java.util的日期-时间API及其格式APISimpleDateFormat已经过时并且容易出错。建议完全停止使用它们,并切换到现代的日期时间API。

  • 无论出于什么原因,如果您必须坚持使用Java6或Java7,您都可以使用ThreeTen-Backport,它将大部分Java.time功能后端到Java6&7。
  • 如果您正在为一个Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查通过desugaring提供的Java8+API,以及如何在Android项目中使用ThreeTenABP。

演示:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        final String strDateTime = "24 Oct 2016 7:31 pm";
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()             // For case-insensitive (e.g. am, Am, AM) parsing 
                .appendPattern("d MMM uuuu h:m a")  // Pattern conforming to the date-time string
                .toFormatter(Locale.ENGLISH);       // Locale
        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
        System.out.println(ldt);
    }
}

输出:

2016-10-24T19:31

默认情况下,datimeformatter#ofpatter使用JVM在启动过程中根据主机环境设置的默认格式区域设置。simpledateformat的情况也是如此。我试图通过以下演示来说明这个问题:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        final String strDateTime = "24 Oct 2016 7:31 pm";           
        DateTimeFormatter dtfWithDefaultLocale = null;          

        System.out.println("JVM's Locale: " + Locale.getDefault());
        // Using DateTimeFormatter with the default Locale
        dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
        System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
        System.out.println(
                "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));

        // Setting the JVM's default locale to Locale.FRANCE
        Locale.setDefault(Locale.FRANCE);
        
        // Using DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
        DateTimeFormatter dtfWithEnglishLocale = getDateTimeFormatterWithEnglishLocale();
        System.out.println("JVM's Locale: " + Locale.getDefault());
        System.out.println("DateTimeFormatter's Locale: " + dtfWithEnglishLocale.getLocale());
        LocalDateTime zdt = LocalDateTime.parse(strDateTime, dtfWithEnglishLocale);
        System.out.println("Parsed with Locale.ENGLISH: " + zdt);

        
        System.out.println("JVM's Locale: " + Locale.getDefault());
        // Using DateTimeFormatter with the default Locale
        dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
        System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
        System.out.println(
                "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
    }
    
    static DateTimeFormatter getDateTimeFormatterWithDefaultLocale() {
        return new DateTimeFormatterBuilder()
                .parseCaseInsensitive()             
                .appendPattern("d MMM uuuu h:m a") 
                .toFormatter(); // Using default Locale
    }
    
    static DateTimeFormatter getDateTimeFormatterWithEnglishLocale() {
        return new DateTimeFormatterBuilder()
                .parseCaseInsensitive()             
                .appendPattern("d MMM uuuu h:m a") 
                .toFormatter(Locale.ENGLISH); // Using Locale.ENGLISH
    }
}
JVM's Locale: en_GB
DateTimeFormatter's Locale: en_GB
Parsed with JVM's default locale: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: en
Parsed with Locale.ENGLISH: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: fr_FR
Exception in thread "main" java.time.format.DateTimeParseException: Text '24 Oct 2016 7:31 pm' could not be parsed at index 3
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at Main.main(Main.java:34)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        final String strDateTime = "24 Oct 2016 7:31 pm";
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy h:m a", Locale.ENGLISH);
        Date date = sdf.parse(strDateTime);
        System.out.println(date);
    }
}
Mon Oct 24 19:31:00 BST 2016
 类似资料:
  • 问题内容: 我收到以下错误:“ java.text.ParseException:无法解析的日期:“ 2011年8月31日09:53:19””具有以下格式: 有人看到这个问题吗? 问题答案: 确保您使用正确的语言环境。(构造函数使用 系统默认的语言环境 ,它可能不是您要使用的 语言环境 。) 这在我的机器上工作正常: (例如,使用时会产生。)

  • 问题内容: 我只是尝试解析一个简单的时间!这是我的代码: 我收到此异常: 问题答案: 这有效: 输出:

  • 问题内容: 如果能找到有关此异常的错误的帮助,我将不胜感激: 和以下代码: 它用引发异常。 ,作为例外。 谢谢。 问题答案: 代表时区字符。需要引用:

  • 问题内容: 我正在获取以下代码 如果我注释掉该行,那么我会在输出中看到时差 我究竟做错了什么?? 问题答案: “ S”为毫秒。每秒有1000毫秒(0到999)。389362大于999。多余的389000毫秒将转换为389秒或6分29秒,并添加到时间中。

  • 问题内容: 以下是引发异常的代码段: 上面的代码在2:00 AM之后的所有日期均抛出异常,它一直运行到01:30 AM 已配置夏令时(我正在使用时区)。 之后,我可能会看到3:00 AM的日志也没有记录在2:00 AM和3:00 AM之间的时间。 日志: 10月01日03:02:01错误:无法解析的日期:“ 201710010200” 引起原因:java.text.ParseException:无

  • 问题内容: 我想将日期解析为所需的格式,但是每次都会收到异常。我知道这很容易实现,但是我遇到了一些问题,不知道确切的位置: 以下是我的代码: 方法输入:2014-06-04 问题答案: 您的字符串中没有时间部分:月份仅替换两个字符 与