在gmt timezone获取上午/下午

时间:2016-02-06 10:15:09

标签: android simpledateformat

我想在gmt timezone中获取am / pm但是代码因此错误而崩溃。如何在GMT上午/下午更改我从timepicker中选择的现有/默认时区?

代码:

 String selectedHour="15";
        String selectedMinute="32";
        String am_pm="PM";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:MM a", Locale.US);
        Date testDate = null;
        try {
            testDate = sdf.parse(selectedHour+" "+selectedMinute+" "+am_pm);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("a");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..." + newFormat);

错误:

System.err: java.text.ParseException: Unparseable date: "15 32 PM" (at offset 2)  
System.err:     at java.text.DateFormat.parse(DateFormat.java:579)

1 个答案:

答案 0 :(得分:2)

您的代码应该像这样解析日期

testDate = sdf.parse(selectedHour+":"+selectedMinute+" "+am_pm);