简体   繁体   中英

How to Extract hours portion from time

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

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]

Use java.time classes, specifically LocalTime .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM