Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hours and minutes from a String variable ?

Tags:

java

string

time

I have a String timme = "13:10". I was wondering how would I best go about getting the hours and minutes and converting them into integers. i.e. int hours = 13 , int minute = 10. I know a for-loop wouldn't be the most efficient way, is there anything simpler?

like image 220
user2219097 Avatar asked Dec 07 '25 08:12

user2219097


1 Answers

Try this way, by split() method,

String timme = "13:10";
String[] time = timme.split ( ":" );
int hour = Integer.parseInt ( time[0].trim() );
int min = Integer.parseInt ( time[1].trim() );
like image 84
user3317558 Avatar answered Dec 08 '25 22:12

user3317558



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!