0

Hello guys I'm new to Java and would like to take this challenge myself, since I'm new I need some help from you.

I have a Java string like this:

String strTime = "8:44 PM";

I would like to get the hr, min and am/pm.. How can I do that? I'm also thinking if the hour is 2-digit? What else should I be considering? Thanks for your help.

UPDATE:

  String[] parts[] = strTime.split(":");
  String strHr = parts[0];
  String strMin = parts[1];
  String strAM = parts[2];

But I don't know how to capture for the other to be split like the space, etc.

user255798
  • 11
  • 1
  • 1
  • 5
  • 2
    You should either parse the String yourself using the functions in the String class, or you can look into regular expressions. – Kevin Workman Mar 19 '14 at 12:46
  • possible duplicate of [How to parse a date?](http://stackoverflow.com/questions/999172/how-to-parse-a-date) – Philipp Mar 19 '14 at 12:47
  • 1
    Have a look at SimpleDateFormat: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – D-rk Mar 19 '14 at 12:48
  • You may want to try this: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – BadIdeaException Mar 19 '14 at 12:48
  • 1
    The correct approach of doing this is parse the String to Date and work with the Date API to get the hour/min etc. – hovanessyan Mar 19 '14 at 12:49
  • @hovanessyan can you expound more on that? Thanks – user255798 Mar 19 '14 at 12:55
  • @user255798 the matter is really hard to explain to new people in Java - first Date's methods are deprecated, you have to use - String - to Date - to Calendar - conversion between objects, it's much easier using 3rd party solutions, but imo it's better to first learn standard language API. In your case the easier solutions will be Guava's Splitter (instead of String split()) and Joda Time (instead of java.util.Date + Calendar) – hovanessyan Mar 19 '14 at 13:40
  • to convert the String to Date - check this -http://stackoverflow.com/a/1154990/1007273 - to convert the Date to Calendar - check this - http://www.vogella.com/tutorials/JavaDateTimeAPI/article.html – hovanessyan Mar 19 '14 at 13:45

4 Answers4

0

What you are doing is very inefficient, however a hardcoded solution:

String[] fistPart = strTime.split(":");
String strHr = firstPart[0];
String[] secondPart = firstPart[1].split(" ");
String strMin = secondPart[0];
String strAM = secondPart[1];

I also suggest looking at regex.

Laurent
  • 1,292
  • 4
  • 23
  • 47
0

Try this piece of code:

    String strTime = "8:44 PM";
    String[] parts = strTime.split(":");
    String strHr = parts[0];
    String rest = parts[1];
    String[] rest2 = rest.split(" ");
    String strMin = rest2[0];
    String strAM = rest2[1];

In variable parts you wil have two Strings:

8 // strHr
44 PM // rest

Then; in rest you will have:

44 // strMin
PM // strAM
ruhungry
  • 4,506
  • 20
  • 54
  • 98
0

You can do this :-

String[] fistPart = strTime.split(" ");
String time = firstPart[0];
String strAM = firstPart[1];
String strHr = time.split(":")[0];
String strMin = time.split(":")[1];


8:44  PM
------------
Time| AM/PM

8:44
----
H|Min
Anish Shah
  • 7,669
  • 8
  • 29
  • 40
0

Firstly u have to set a fix time format that it should be in 2 digit or 1 digit. like the time is 08:44 AM or 8:44 AM .

I think u choose the the 2 digit time format for the responsivre programming .

Then u put the logoc for extract the time from time format.

then follow the following code

String strTime = "8:44 PM";
String hour= strTime.split(":")[0];
String remain = strTime.split(":")[1];
String min= remain.split(" ")[0];
String am_pm= remain.split(":")[1];