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.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information