Home > Net >  How to Extract hours portion from time
How to Extract hours portion from time

Time:11-30

I have time "22:00:30" in String format I want to just extract the hour 22 from it. Can I get some help. I tried searching but everywhere DateTime has been used whereas in my case there is no date

CodePudding user response:

string.split(":") will return a list of each of the substrings that were separated by the : character. Try this:

s = "22:00:30"
hour = s.split(":")[0]

CodePudding user response:

Use java.time classes, specifically LocalTime.

int hour = LocalTime.parse( "22:00:30" ).getHour() ;

CodePudding user response:

Parse it into a java.time.LocalTime object. Then use the getHour() method to extract hour value.

LocalTime time = LocalTime.parse("22:00:30");
System.out.print(time.getHour()); //to get hour value
  • Related