Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:45:39+00:00 2026-06-10T00:45:39+00:00

How can I parse a time of format hh:mm:ss , inputted as a string

  • 0

How can I parse a time of format hh:mm:ss , inputted as a string to obtain only the integer values (ignoring the colons) in java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:45:40+00:00Added an answer on June 10, 2026 at 12:45 am

    As per Basil Bourque’s comment, this is the updated answer for this question, taking into account the new API of Java 8:

        String myDateString = "13:24:40";
        LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss"));
        int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
        int minute = localTime.get(ChronoField.MINUTE_OF_HOUR);
        int second = localTime.get(ChronoField.SECOND_OF_MINUTE);
    
        //prints "hour: 13, minute: 24, second: 40":
        System.out.println(String.format("hour: %d, minute: %d, second: %d", hour, minute, second));
    

    Remarks:

    • since the OP’s question contains a concrete example of a time instant containing only hours, minutes and seconds (no day, month, etc.), the answer above only uses LocalTime. If wanting to parse a string that also contains days, month, etc. then LocalDateTime would be required. Its usage is pretty much analogous to that of LocalTime.
    • since the time instant int OP’s question doesn’t contain any information about timezone, the answer uses the LocalXXX version of the date/time classes (LocalTime, LocalDateTime). If the time string that needs to be parsed also contains timezone information, then ZonedDateTime needs to be used.

    ====== Below is the old (original) answer for this question, using pre-Java8 API: =====

    I’m sorry if I’m gonna upset anyone with this, but I’m actually gonna answer the question. The Java API’s are pretty huge, I think it’s normal that someone might miss one now and then.

    A SimpleDateFormat might do the trick here:

    http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    It should be something like:

    String myDateString = "13:24:40";
    //SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    //the above commented line was changed to the one below, as per Grodriguez's pertinent comment:
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse(myDateString);
    
    Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
    calendar.setTime(date);   // assigns calendar to given date 
    int hour = calendar.get(Calendar.HOUR);
    int minute; /... similar methods for minutes and seconds
    

    The gotchas you should be aware of:

    • the pattern you pass to SimpleDateFormat might be different then the one in my example depending on what values you have (are the hours in 12 hours format or in 24 hours format, etc). Look at the documentation in the link for details on this

    • Once you create a Date object out of your String (via SimpleDateFormat), don’t be tempted to use Date.getHour(), Date.getMinute() etc. They might appear to work at times, but overall they can give bad results, and as such are now deprecated. Use the calendar instead as in the example above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I parse a string that represents a date and/or time using iPhone
How can I parse a date/time string into an Access Date object given a
I'm looking for a JavaScript library which can parse and format date values and
Is there any tool that can parse/conver xml files to this format? I have
I have string of format 01-Jan-11 and I need to parse it to a
I am trying to parse a vector of time string and came across a
I'm trying to format a time/date string: String date = 2011-07-27T06:41:11+00:00; DateFormat formatter =
How can I parse this date format that my web service is receiving in
I am being returned this time format from an API: 1287498792000 Can anyone advise
How i can parse and extract the parameters from an SQL Query using delphi?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.