1

I have a scanner String input in the following format: 12:00:00PM.

I managed to finally isolate PM or AM by this:

public class ConverTime
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String timeString = sc.next();

        String[] pmOrAm = timeString.split("\\d");
        String[] nums = timeString.split("(: + \\D)");

        // PM or AM
        String timeType = pmOrAm[pmOrAm.length - 1];
        for (String n: nums)
        {
            System.out.println(n);
        }
    }
} 

I know the way I isolate PM or AM is not the best, so hopefully there is a better way where i can get an array of exactly one string.

But when I run the for loop for the "nums" array, the output is:

 12:00:00PM

I want the output to be like so:

12
00
00

without the "PM" or "AM." How do I go about this?

5 Answers 5

3

Just for completeness sake, while there have been good solutions for split and regular expressions, there is another one for your use case...

Data date = new SimpleDateFormat("HH:mm:ssaa").parse(s);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println( data.get(Calendar.HOUR);
System.out.println( data.get(Calendar.MINUTE);
System.out.println( data.get(Calendar.SECOND);

Just as an idea in a different direction, since we are talking about time Strings here, which would allow you to make assumptions about it beyond the pure regular expression format...

Sign up to request clarification or add additional context in comments.

Comments

2

an easy way could be:

timeString.split("(AM|PM|:)")

Hope it helps

2 Comments

This worked! Thanks! Although I was looking to do it purely use the split method, this is working too!
Thank you, that is even much simpler!! I upvoted you but my upvote won't count yet.
2

Like this:

(\d+):(\d+):(\d+)(?:[AP]M)

See a demo on regex101.com.
Remember to double escape the backslashes in Java (ie \\d+).

2 Comments

Are you sure? This out puts nothing on the screen now. I did the following: String[] nums = timeString.split("(\d+):(\d+):(\d+)(?:[AP]M)");
@NishaShlaymoon: with .split(), you can even use the colon.
1

you just have to use String[] nums = timeString.split(":"); and to output each index it would be something like System.out.println(nums[0]); , nums[1] and nums[2] and to remove AM/PM you can do String removeap = nums[2].substring(0, 2);

Comments

1

I think more correct way is to use regular expression here rather than split. This way you can detect if you get unexpected inputs rather than having the string processed incorrectly.

    String s = "12:01:02PM";
    Pattern pat = Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)\\w\\w");
    Matcher matcher = pat.matcher(s);

    if (!matcher.matches()) {
        // Handle error...
    }

    int hh = Integer.valueOf(matcher.group(1));
    int mm = Integer.valueOf(matcher.group(2));
    int ss = Integer.valueOf(matcher.group(3));

    System.out.println(hh + ", " + mm + ", " + ss);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.