【问题标题】:convert string to date with locale [duplicate]使用语言环境将字符串转换为日期[重复]
【发布时间】:2018-04-28 07:27:44
【问题描述】:

我正在尝试将字符串转换为日期,但每次我这样做时都会向我抛出错误,我不确定我错过了什么

    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
    try {
        // m brings in all variables from a get/setter
        date = format.parse(m.getEventTime());
    } catch (ParseException e) {
        e.printStackTrace();
        Log.d("Response.Error.Date", m.getEventTime());
    }
    eventTime.setText(date.toString());

我的变量 m.getEventTime() 正在传递以下字符串 2018-04-28 14:00:00

我尝试将字符串作为2018-04-28T14:00:00Z 传递,但无济于事。

try/catch 块的堆栈跟踪没有错误,但日志正在打印出D/Response.Error.Date: 2017-08-19 15:00:00 当我将 e.toString() 添加到日志时,它会打印出D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"

在实际应用程序中,运行时间显示为now Sat Apr 28 08:22:33 GMT+01:00 2018

我错过了什么吗?

【问题讨论】:

  • 简单的日期格式应该是“yyyy-MM-dd hh:mm:ss”来解析那个字符串
  • @JyotiJK 谢谢,我认为 new SimpleDateFormat("EEE, MMM d, yy hh:mm a" 是如何解析的 - 更改为您的方式允许我实际解析日期 - 但现在我将如何将其更改为 EEE, MMM d, yy hh:mm a 格式?跨度>
  • 获取日期后,再次将日期更改为此格式new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);。结果将是字符串而不是日期。
  • A Date 没有格式,因此要获得所需的格式,您需要使用新的格式化程序将其格式化为新字符串。顺便考虑一下扔掉早已过时且臭名昭著的麻烦 SimpleDateFormat 和朋友,并将 ThreeTenABP 添加到您的 Android 项目中,以便使用现代 Java 日期和时间 API java.time。使用起来感觉好多了。
  • 我认为这是一个非常严重的问题,您看不到正在打印的堆栈跟踪。你应该考虑修复你的项目设置,这样你就可以看到你的程序试图告诉你发生的错误。

标签: java android datetime simpledateformat datetime-parsing


【解决方案1】:

你可以试试这个,

Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
    // m brings in all variables from a get/setter
    date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
    e.printStackTrace();
    Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));

【讨论】:

    【解决方案2】:

    你可以得到这样的时间格式:

        String dateTime = null;
    
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
                                  //here you can use your required format 
    
        dateTime = df.format(new Date(stringTime));
    

    【讨论】:

      【解决方案3】:

      您正在将日期转换为导致异常的英文:

      请更改:

      SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
      
      eventTime.setText(date.toString());
      

      与

      SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");
      
      eventTime.setText(format.format(date));
      

      【讨论】:

      • 答案不正确,抱歉。随着您的更改,仍然会引发相同的异常。
      • 对不起!请更改“eventTime.setText(date.toString());”到“eventTime.setText(format.format(date));”也是。
      • Prakhar Gupta,如果你认为你对ParseException 的原因有一个答案,你可以edit your answer。这比在评论中添加信息要好。
      猜你喜欢
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2014-03-09
      • 2019-10-09
      • 2014-03-30
      • 2012-06-04
      • 1970-01-01
      • 2013-10-17
      相关资源
      最近更新 更多