【问题标题】:"Thu Jul 21 07:00:00 EDT 2016" change date format android to MM/dd/yyyy h:mm a“Thu Jul 21 07:00:00 EDT 2016”将日期格式 android 更改为 MM/dd/yyyy h:mm a
【发布时间】:2016-06-22 06:00:55
【问题描述】:

我尝试下面的代码,但它给出了如何更改日期格式的异常,例如 6/21/2016 7:00 PM

    public static String parseDate(String time) {

    String outputPattern = "MM/dd/yyyy h:mm a";
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}

【问题讨论】:

  • 请发布您的堆栈跟踪...
  • 请澄清:给定的字符串time,这是6/21/2016 7:00 PM,就像您在问题中描述的那样?或者这就是你想要的?
  • 无法解析的日期:“Thu Jul 21 07:00:00 EDT 2016”(偏移量 0)
  • 输入字符串:“Thu Jul 21 07:00:00 EDT 2016”并希望输出为“6/21/2016 7:00 PM”
  • 然后按照 Sanjeev 的回答,那是正确的。您创建了错误的输入格式字符串。

标签: android datetime


【解决方案1】:

您需要使用以下模式解析您的输入字符串

SimpleDateFormat inputFormat = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss Z yyyy");

这将解决您的 UnParseableDate 异常。

【讨论】:

  • 很高兴为您提供帮助。玩得开心编码:)
  • 我们可以只得到时间而不分割字符串吗?如果给我解决方案
  • 是的,使用String outputPattern = "h:mm a";
  • 如何验证结束时间日期不大于开始时间和日期
  • 请单独提出一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 2015-02-03
  • 2019-07-19
  • 1970-01-01
  • 2013-12-10
  • 2019-08-25
相关资源
最近更新 更多