如何从时间戳20180805231400 -0300中获取格式为HH:mm:ss的时间

时间:2018-08-10 12:01:14

标签: android time timezone simpledateformat timezone-offset

下面是我的代码,但它引发异常,表明无法解析日期格式。

     try{

        DateFormat outputFormat = new SimpleDateFormat("yyyyMMddHHmmss zzzz", Locale.getDefault());
        DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.getDefault());

        String inputText = channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
        Date date = inputFormat.parse(inputText);
        String outputText = outputFormat.format(date);

      } catch (Exception e) {
         e.printStackTrace();
      }

2 个答案:

答案 0 :(得分:0)

我希望您可以这样做

    oldfmt = "yyyyMMddHHmmss zzzz";
    newfmt = "HH:mm:ss";
    getTimeFromUtc(oldfmt, newfmt, "20180805231400 -0300");

public void getTimeFromUtc(String oldFormat, String newformat, String datetime) {
    SimpleDateFormat inputFormat = new SimpleDateFormat(oldFormat);
    inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat outputFormat = new SimpleDateFormat(newformat, Locale.getDefault());
    TimeZone defaultTimezone = TimeZone.getDefault();
    outputFormat.setTimeZone(defaultTimezone);
    String inputDateStr = datetime;
    Date date = null;
    try {
        date = inputFormat.parse(inputDateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (date != null) {
        outputDateStr = outputFormat.format(date);
    }
    Toast.makeText(getApplicationContext(), outputDateStr, Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

尝试一下

try{

 DateFormat inputFromat = new SimpleDateFormat("yyyyMMddHHmmss zzzz", Locale.getDefault());
 DateFormat outputFormat= new SimpleDateFormat("HH:mm:ss", Locale.getDefault());

 String inputText = channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
                Date date = inputFormat.parse(inputText);
                String outputText = outputFormat.format(date);
} catch (Exception e) {
        e.printStackTrace();
}