How to Parse Time in Format hh:mm:ss in Java? | Answered
If you want to parse a time in the format "hh:mm:ss" in Java, you can use the SimpleDateFormat
class. Here is an example:
String time = "12:34:56";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(time);
In this example, we create a String
variable called time
that contains the time in the format "hh:mm:ss". We then create a new instance of the SimpleDateFormat
class and specify the format "HH:mm:ss", which corresponds to the "hh:mm:ss" format. Finally, we call the parse
method on the SimpleDateFormat
object and pass in the time
variable, which converts the String
into a Date
object.
You can then use the getTime
method on the Date
object to get the time in milliseconds, or use the format
method on the SimpleDateFormat
object to convert the Date
object back into a String
with a different format.
Overall, parsing time in the format "hh:mm:ss" in Java is easy using the SimpleDateFormat
class. Just create a new instance of the class with the correct format, and call the parse
method to convert the String
into a Date
object.
Leave a Reply
Related posts