Vic Torious
Vic Torious

Reputation: 1947

Separating hours from minutes in a given time value

I'm currently working on a simple Time-Manager Application for Android Devices. My Problem: I am getting a time value (that looks like this -> 6:51) from a Server. Now I want to separate the hours and minutes and I want the value to be updated continuously.

I have already looked into joda-time but cannot find anything myself that would solve my problem, if there is a solution in joda-time at all.

Should I try extracting the digits and build my time-format out of them or is there a better and simpler solution? In case of you recommending me to extract the digits, how do I solve the problem with hours above 9.

Thanks for Help and sorry for bad english.

Upvotes: 3

Views: 5126

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 338876

In Joda-Time 2.5.

LocalTime localTime = LocalTime.parse( "6:51" );
int hour = localTime.getHourOfDay();
int minute = localTime.getMinuteOfHour();

Upvotes: 0

Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

Are you getting time from server as String "6:51" ?

org.joda.time.LocalTime#parse(String) will help you. LocalTime represent time without date. After parsing String you will be able to call methods getHourOfDay,getMinuteOfHour.

Upvotes: 1

Tarek
Tarek

Reputation: 771

Just parse the value of time as String and use split to separate hours and minutes. Then, you can convert it to int again for future uses.

String Time = (String) time;
String Hour=time.split(":")[0];
String Minute=time.split(":")[1];
//If you want to use Hour and Minute for calculation purposes:
int hour=Integer.parseInt(Hour);
int minute=Integer.parseInt(Minute);

There should be no problem, if hours>9

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44844

If the string that you have is in the format hh:mm, then you can use String.split to separate them.

String arr [] = time.split(":");

Upvotes: 1

Ravi Sharma
Ravi Sharma

Reputation: 843

Split the time.

 String time="6:51"              //which is from server;
 String splitTime[]=time.split(":");
 String hours=splitTime[0];
 String minutes=splitTime[1];

Upvotes: 5

Related Questions