2011-09-15 38 views
4

我有一个string以这种格式保存开始时间和结束时间8:30AM - 9:30PM我希望能够去除AM -PM并将所有时间转换为24小时格式所以9:30PM真的是21:30,也有两个不同的变量存储时间,我知道如何去掉substrings字符串,但林不知道转换,这是我迄今为止。时间变量开始持有8:30AM - 9:30PM将时间串转换为24小时格式

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" 

回答

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)); 
    } 
} 

现在我想想,SimpleDateFormatH h K k可能会造成混淆。

干杯。

+0

谢谢!效果很好。 – Beef

+0

我回到这个与另一个问题,我仍然使用你的方法来转换的时间,但我想知道你是否知道如何转换后,我可以减去一小时的时间,我不能这样做在原因林只做一段时间 – Beef

+0

牛肉,我建议你为它打开另一个问题,但基本上你可以使用'TWENTY_FOUR_TF.parse'和你的字符串重新获得'Date'对象,然后将它发送到一个'日历'使用'calendar.setTime'。用'calendar.add'减去一小时,用'calendar.getTime'返回时间,并使用'TWENTY_FOUR_TF.format'来获得生成的'String'。所以字符串(解析) - >日期 - >日历 - >数学运算 - >日期(格式) - >字符串。 –

3

我不会重新发明轮子(除非你正在做这样的学校项目或一些这样的)。

只是得到一个日期对象分配自己的时间戳记,然后就可以生成你想要这个任何格式:SimpleDateFormat

[编辑以满足您的具体要求] 如果你确实需要从自己独特的工作字符串,那么你会做这样的事情(我不知道你的字符串是什么样的......你使用的偏移量是85,这意味着没有任何背景)。

我没有检查这个窃听器,但是这大约是你想要的...

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 
+0

以及我从一个文本文件中读取这些时间和这就是为什么它们会在子一起阅读,然后得到安置到其他2子,因为我需要转换他们24小时,然后我会使用ODBC将它们放入数据库,该文本文件包含几百个不同的开始和结束时间,以便做一些调度 – Beef

+0

我解释了我的'时间'变量持有,所以我没有认为人们会需要担心我的偏移 – Beef

4

您需要使用:SimpleDateFormat
可以参考这个教程:Formatting hour using SimpleDateFormat

例:

//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)); 
1

24小时的时间增加12到任何时间大于12pm,所以下午1点是13等等24点或12点。这里是须藤代码:

if(hour <= 12) 
{ 
    hour = hour + 12; 
} 
+0

我明白如何做到这一点,而不是如何编写它,而不使用'substrings'来检查其am或pm,然后得到一个'substring '在AM或PM之前的时间,然后不得不转换那个时间,我觉得这会让我有不必要的'子字符串'@ToddMoses – Beef

1

随着Joda Time,代码如下:

 DateTimeFormatter formatter12 = DateTimeFormat.forPattern("K:mma"); 
     DateTime begin = formatter12.parseDateTime(beginTime); 
     DateTime end = formatter12.parseDateTime(endTime); 

     DateTimeFormatter formatter24 = DateTimeFormat.forPattern("k:mma"); 
     String begin24 = formatter24.print(begin); 
     String end24 = formatter24.print(end); 
相关问题