将 Java 字符串转换为时间,而不是日期 [英] Convert Java string to Time, NOT Date

查看:26
本文介绍了将 Java 字符串转换为时间,而不是日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将变量字符串转换为时间类型变量,而不是使用 Java 的日期.字符串看起来像这样 17:40

I would like to convert a variable string to a Time type variable, not Date using Java. the string look like this 17:40

我尝试使用下面的代码,但这个实例是一个日期类型变量而不是时间

I tried using the code below but this instance is a date type variable not time

String fajr_prayertime  =       prayerTimes.get(0);
DateFormat formatter = new SimpleDateFormat("HH:mm");
fajr_begins = (Date)formatter.parse(fajr_prayertime);
System.out.println(" fajr time " + fajr_begins);

然而,Netbean 抱怨我应该插入如下异常;

However Netbean complains that I should insert an exception as below;

DateFormat formatter = new SimpleDateFormat("HH:mm");
try {
fajr_begins = (Date)formatter.parse(fajr_prayertime);
} catch (ParseException ex) {
Logger.getLogger(JavaFXApplication4.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" fajr time " + fajr_begins);

知道如何从上面的字符串中获取时间.

Any idea how I can get the time out of the string above.

推荐答案

java.sql.Time timeValue = new java.sql.Time(formatter.parse(fajr_prayertime).getTime());

这篇关于将 Java 字符串转换为时间,而不是日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆