将一串时间转换为24小时格式

Bee*_*eef 5 java datetime substring

我有一个string拿着开始时间,在这种格式的结束时间,8:30AM - 9:30PM我希望能带出的AM -PM所有的时间转换到24小时格式所以9:30PM真的是21:30,也有两种存储在2级不同的变量的时候,我知道如何剥离字符串,substrings但我不确定转换,这是我到目前为止.时间变量开始持有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"
Run Code Online (Sandbox Code Playgroud)

Ant*_*oly 8

工作代码(考虑到你设法拆分字符串):

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));
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想到它,SimpleDateFormat,H h K k可能会令人困惑.

干杯.