将 Java 字符串转换为时间,而不是日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18604408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Java string to Time, NOT Date
提问by Ossama
I would like to convert a variable string to a Time type variable, not Date using Java. the string look like this 17:40
我想将变量字符串转换为时间类型变量,而不是使用 Java 的日期。字符串看起来像这样 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);
However Netbean complains that I should insert an exception as below;
然而,Netbean 抱怨我应该插入如下异常;
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.
知道如何从上面的字符串中获得时间。
采纳答案by ash
java.sql.Time timeValue = new java.sql.Time(formatter.parse(fajr_prayertime).getTime());
回答by Michael 'Maik' Ardan
You might want to take a look at this example:
你可能想看看这个例子:
public static void main(String[] args) {
String myTime = "10:30:54";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = null;
try {
date = sdf.parse(myTime);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedTime = sdf.format(date);
System.out.println(formattedTime);
}
回答by Matt Johnson-Pint
回答by Shivam
You can use the following code for changing the String value into the time equivalent:
您可以使用以下代码将 String 值更改为等效的时间:
String str = "08:03:10 pm";
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = (Date)formatter.parse(str);
Hope this helps you.
希望这对你有帮助。
回答by Muhammad Suleman
try...
尝试...
java.sql.Time.valueOf("10:30:54");
回答by Basil Bourque
Joda-Time & java.time
Joda-Time & java.time
Both Joda-Timeand java.time(new in Java 8) offer a LocalTime
class to represent a time-of-day without any date or time zone.
无论乔达时间和java.time(新Java中8)提供了一个LocalTime
类来表示时间的天,没有任何日期或时区。
Example code in Joda-Time 2.3.
Joda-Time 2.3 中的示例代码。
LocalTime localTime = new LocalTime( "14:40" );
LocalTime deadline = new LocalTime( "15:30" );
boolean meetsDeadline = localTime.isBefore( deadline );
回答by Paris1008
try {
SimpleDateFormat format = new SimpleDateFormat("hh:mm a"); //if 24 hour format
// or
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); // 12 hour format
java.util.Date d1 =(java.util.Date)format.parse(your_Time);
java.sql.Time ppstime = new java.sql.Time(d1.getTime());
} catch(Exception e) {
Log.e("Exception is ", e.toString());
}
回答by JanB
String to Time (using an arbitrary time):
字符串到时间(使用任意时间):
String myTime = "10:00:00";
Time startingTime = new Time (myTime);
String to Time (using currentTime):
字符串到时间(使用 currentTime):
String currentTime = getCurrentTime();
Time startingTime = new Time (currentTime);
Time to String:
字符串时间:
private String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("kkmmss");
String currentTime = dateFormat.format(System.currentTimeMillis());
return currentTime;
}
回答by Khusboo
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM");
simpleDateFormat.format(fajr_prayertime);