简体   繁体   中英

Parse time range from string

I couldn't find an answer to my exact problem so here it goes...Lets say I have a string array with time values that I want to convert to 24-hour time like so:

String[] strings = {"12:04 am", "12:04 pm"};
System.out.println("Pick-up Times: ");
for (String i: strings) 
    System.out.println(i);
System.out.println();

DateFormat format = new SimpleDateFormat("hh:mm a");

System.out.println("Converted to 24-Hour format: ");
for (String i: strings) {
    Date date = format.parse(i);        
    Time time = new Time(date.getTime());
    System.out.println(time);
}

Output:

Pick-up Times:
12:04 am
12:04 pm

Converted to 24-Hour format:
00:04:00
12:04:00

That works great, but let's say I have a range like 12:00 pm - 12:10 pm for example:

String[] strings = {"12:00 pm - 12:10 pm"};
System.out.println("Pick-up Times: ");
for (String i: strings) 
    System.out.println(i);
System.out.println();

DateFormat format = new SimpleDateFormat("hh:mm a");

System.out.println("Converted to 24-Hour format: ");
for (String i: strings) {
    Date date = format.parse(i);        
    Time time = new Time(date.getTime());
    System.out.println(time);
}

Here's my output:

Pick-up Times:
12:00 pm - 12:10 pm

Converted to 24-Hour format:
12:00:00

I've tried changing SimpleDateFormat like so:

DateFormat format = new SimpleDateFormat("hh:mm a - hh:mm a");

And the result was interesting:

Pick-up Times:
12:00 pm - 12:10 pm

Converted to 24-Hour format:
12:10:00

The result I'm looking for is:

Pick-up Times:
12:00 pm - 12:10 pm

Converted to 24-Hour format:
12:00:00 - 12:10:00

Any ideas?

This is OPs solution (extracted from the question):


The solution was pretty simple. For anyone that runs into the same problem, this will do the trick:

String[] strings = {"12:00 pm - 12:10 pm"};
System.out.println("Pick-up Times: ");
for (String i: strings) 
    System.out.println(i);
System.out.println();

DateFormat format = new SimpleDateFormat("hh:mm a");
int count = 0;

System.out.println("Converted to 24-Hour format: ");
for (String i: strings) {
    String[] test = i.split(" - ");
    for (String j: test)
    {
        Date date = format.parse(j);        
        Time time = new Time(date.getTime());
        System.out.print(time);
        if (count == 0)
            System.out.print(" - ");
        count++;
    }
    System.out.println();
}

Ouput:

Pick-up Times:
12:00 pm - 12:10 pm

Converted to 24-Hour format:
12:00:00 - 12:10:00

Well, if the problem is about the range, you can split it up. I'll give you a few lines of code, so you adapt it to your code:

String s = {"ABC - aaa - eee - ccc"};
String arr[] = s.split(" - ");
for(String str: arr)
    SYstem.out.println(str);

Instead of s , you can use your strings .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM