Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a string of time to 24 hour format

I have a string holding a start time and an end time in this format 8:30AM - 9:30PM I want to be able to strip out the AM - and the PM and convert all the times to 24 hour format so 9:30PM would really be 21:30 and also have both the times stored in 2 different variables, I know how to strip the string into substrings but Im not sure about the conversion, this is what I have so far. the time variable starts out holding 8:30AM - 9:30PM.

String time = strLine.substring(85, 110).trim();
//time is "8:30AM - 9:30PM" 

String startTime;               
startTime = time.substring(0, 7).trim();
//startTime is "8:30AM"

String endTime;
endTime = time.substring(9).trim();
//endTime "9:30AM"
like image 528
Beef Avatar asked Sep 15 '11 17:09

Beef


3 Answers

Working code (considering that you managed to split the Strings):

public class App {
  public static void main(String[] args) {
    try {
        System.out.println(convertTo24HoursFormat("12:00AM")); // 00:00
        System.out.println(convertTo24HoursFormat("12:00PM")); // 12:00
        System.out.println(convertTo24HoursFormat("11:59PM")); // 23:59
        System.out.println(convertTo24HoursFormat("9:30PM"));  // 21:30
    } catch (ParseException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  // Replace with KK:mma if you want 0-11 interval
  private static final DateFormat TWELVE_TF = new SimpleDateFormat("hh:mma");
  // Replace with kk:mm if you want 1-24 interval
  private static final DateFormat TWENTY_FOUR_TF = new SimpleDateFormat("HH:mm");

  public static String convertTo24HoursFormat(String twelveHourTime)
        throws ParseException {
    return TWENTY_FOUR_TF.format(
            TWELVE_TF.parse(twelveHourTime));
  }
}

Now that I think about it, SimpleDateFormat, H h K k can be confusing.

Cheers.

like image 70
Anthony Accioly Avatar answered Nov 14 '22 05:11

Anthony Accioly


You need to use: SimpleDateFormat
And can refer this tutorial: Formatting hour using SimpleDateFormat

Example:

//create Date object
Date date = new Date();

//formatting hour in h (1-12 in AM/PM) format like 1, 2..12.
String strDateFormat = "h";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

System.out.println("hour in h format : " + sdf.format(date));
like image 4
zengr Avatar answered Nov 14 '22 05:11

zengr


I wouldn't reinvent the wheel (unless you are doing this as a school project or some such).

Just get a date object out of your time stamp and then you can generate whatever format you want with this: SimpleDateFormat

[edited to address your specific request] if you absolutely need to work from your own unique strings, then you'll do something like this (I don't know exactly what your strings look like... you're using offsets like 85, which means nothing out of context).

I didn't check this for bugs, but this is approximately what you want...

myStr = timestampString.toLowerCase(); //something like 8:30am
boolean add12 = (myStr.indexOf("pm") != -1)?true:false;

//convert hour to int
int hour = Integer.parseInt(myStr.split(":")[0]);

int minutes = Integer.parseInt(  myStr.split(":")[1].replaceAll("\\D+","").replaceAll("^0+","") ); //get the letters out of the minute part and get a number out of that, also, strip out leading zeros

int militaryTime = hour + (add12)? 12:0;
if(!add12 && militaryTime == 12)
 militaryTime = 0; //account for 12am

//dont' forget to add the leading zeros back in as you assemble your string
like image 3
Yevgeny Simkin Avatar answered Nov 14 '22 05:11

Yevgeny Simkin