This question already has an answer here:
- Parse clock time in java 8 1 answer
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);
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.
try...
String to Time (using an arbitrary time):
String to Time (using currentTime):
Time to String:
Joda-Time & java.time
Both Joda-Time and java.time (new in Java 8) offer a
LocalTime
class to represent a time-of-day without any date or time zone.Example code in Joda-Time 2.3.
You might want to take a look at this example: