java.text.ParseException 正在将字符串转换为日期

java.text.ParseException while converting string to date

我是 java 的初学者。我是这个网站的新手。

我正在尝试将字符串转换为日期,但出现解析异常。 下面是我的代码: 我的变量 myDateValue 中的值为 Wed May 15 00:00:00 IST 2013

DateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
java.util.Date myDate = sdf1.parse(myDateValue);
java.sql.sqlDate = new java.sql.Date(myDate.getTime());

我得到以下异常:

java.text.ParseException: Unparseable date: "Wed May 15 00:00:00 IST 2013"

我也试过了,但还是不行:

DateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String formatdate = sdf1.format(myDateValue);
java.util.Date myate = sdf1.parse(formatdate);
java.sql.sqlDate = new java.sql.Date(myate.getTime());

为此我得到以下错误:

java.lang.IllegalArgumentException: Cannot format given Object as a Date

我做错了什么?

您的格式 dd-MMM-yyyy 有误。你的日期看起来像 Wed May 15 00:00:00 IST 2013,所以你的格式应该是 - EEE MMM dd HH:mm:ss zzz yyyy.

有关模式字母的详细信息,请参阅 javadoc

编辑以解决评论。如果您希望日期采用 dd-MMM-yyyy 格式,则必须重新设置格式 -

SimpleDateFormat originalFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = originalFormat.parse(myDateValue);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MMM-dd");
String myNewDate = newFormat.format(date);

tl;博士

LocalDate.parse(                    // Represent a date-only value, without time-of-day and without time zone.
    "23-Jan-2018" ,
    DateTimeFormatter.ofPattern(    // Define a formatting pattern to match your input.
        "dd-MMM-uuuu" ,             // The modern parsing codes have changed a bit from legacy codes. Study the class documentation carefully.
        Locale.US                   // Specify a locale to determine a human language to use in translating the name of the month.
    )                               
)                                   // Returns a `LocalDate` object.
.toString()                         // Generate text in standard ISO 8601 format to represent the value of this `LocalDate` object.

看到这个code run live at IdeOne.com

2018-01-23

java.time

现代方法使用 java.time classes 取代了可怕的旧日期时间 classes (Date, Calendar, SimpleDateFormat).

旧的 classes 缺少 class 来表示没有时间和时区的仅日期值。 java.sql.Date class 假装这样做,但实际上保留了一个时刻,一个带有时间和与 UTC 的偏移量的日期。

LocalDate

LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

定义格式模式以匹配您的输入。

String input = "23-Jan-2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

从哪里获得java.time classes?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.