3

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.

Vic Torious
  • 1,757
  • 3
  • 21
  • 24
  • possible duplicate of [Joda Time minutes in a duration or interval](http://stackoverflow.com/questions/9214630/joda-time-minutes-in-a-duration-or-interval) – fejese Oct 22 '14 at 08:33

5 Answers5

5

Split the time.

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

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(":");
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

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.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
0

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

Tarek
  • 771
  • 7
  • 18
0

In Joda-Time 2.5.

LocalTime localTime = LocalTime.parse( "6:51" );
int hour = localTime.getHourOfDay();
int minute = localTime.getMinuteOfHour();
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154