java.text.ParseException:无法解析的日期:“8:30 AM”

java.text.ParseException: Unparseable date: "8:30 AM"

我正在尝试解析“8:30 AM”,但出现无法解析的日期异常。

从我的 UI 那边,我得到“8:30 AM”和“6:30 PM”类型的值,但我必须将该字符串转换为日期格式并将该日期保存在我的数据库中。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {

    public static void main(String[] args) {

        SimpleDateFormat timingFormat = new SimpleDateFormat("h a",
            Locale.US);
        String dateInString = "8:30 AM";

        try {
            // This line throws Unparseable exception
            Date date = timingFormat.parse(dateInString);
            System.out.println(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

来自Documentation

the year value of the parsed Date is 1970 with GregorianCalendar if no year value is given from the parsing operation. The TimeZone value may be overwritten, depending on the given pattern and the time zone value in text.

试试这个

         SimpleDateFormat timingFormat = new SimpleDateFormat("h a", Locale.US);
         Date date = timingFormat.parse("8 AM");
         System.out.println(date.toString());

输出

Thu Jan 01 08:00:00 IST 1970

更新

要获取今天的日期,你可以在解析后尝试这样的操作

     int hours = date.getHours();
     Date today = new Date();
     today.setHours(hours);
     System.out.println(today);

注意 getHours()setHours 已弃用 methods.Its 建议去 Calendar。您必须设置时间, 分钟明确。

更新

如果输入是8:30左右,那么你必须像这样解析它

         SimpleDateFormat timingFormat = new SimpleDateFormat("h:mm a", Locale.US);
         Date date = timingFormat.parse("8:30 AM");
         System.out.println(date.toString());

输出

Thu Jan 01 08:30:00 IST 1970

根据输入的不同,你需要select你是哪种format insterested.You可以检查字符串是否包含:,基于此你可以使用 SimpleDateFormat.

只是为了解析 8:30 AM 只需将上面的格式化程序更改为

SimpleDateFormat timingFormat = new SimpleDateFormat("h:mm a");

此致

已接受的答案正确但已过时。

如前所述,问题有一个输入字符串,仅表示一天中的时间,而 java.util.Date class 表示日期 plus一天中的某个时间。

您需要的是 class 仅代表一天中的某个时间。在 Java 8.

之前的旧版本 Java 中没有这样的 class

java.time

java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Tutorial.

LocalTime

java.time class 包括 LocalTime class。 class 表示没有任何日期和时区的时间。

请注意,formatter we specified a Locale 使用英语正确识别字符串 AMPM,这可能因人类语言而异。

String input = "8:30 AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "h:m a" , Locale.ENGLISH );
LocalTime localTime = LocalTime.parse ( input , formatter );

转储到控制台。

System.out.println ( "localTime: " + localTime );

localTime: 08:30

数据库

接下来,数据库。最终我们应该看到 JDBC drivers updated to directly handle the java.time types. Until then, we must convert from java.time types to the old java.sql types

java.time 转换为 java.sql

对于这个问题,这意味着 java.sql.Time class. That old java.sql.Time class has a new method for convenient conversions, valueOf

java.sql.Time sqlTime = java.sql.Time.valueOf ( localTime );

从那里,JDBC 驱动程序从 java.sql 时间转换为数据库类型。对于这个问题,这可能意味着标准 TIME SQL type

通过 JDBC 驱动程序

将 java.sql.* 对象传递到数据库

通过传递上面看到的 sqlTime 变量,使用 PreparedStatement 插入或更新您的数据。在 whosebug.com 中搜索 SQL.

中无数此类 insert/update 作品的例子