转换 Android 中的日期

huangapple go评论81阅读模式
英文:

convert date in android

问题

我想转换这个日期,但是解析例外,结果应该像这样:"2019-12-28 14:00:00"

try {
    String strCurrentDate = "April 08 2020 4:24 AM";
    SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy  hh:mm a");
    Date newDate = format.parse(strCurrentDate);
    format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
    String date = format.format(newDate);

    Log.d("datessscc", date);
} catch (ParseException e) {
    e.printStackTrace();
}
英文:

I want to convert this date but parse exemption it should be like this result "2019-12-28 14:00:00"

 try {
        String strCurrentDate = "April 08 2020 4:24 AM"
        SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy  hh:mm a");
        Date newDate = format.parse(strCurrentDate);
        format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
        String date = format.format(newDate);

        Log.d("datessscc", date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

答案1

得分: -1

请更改您的 SimpleDateFormat

试试这样:

SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date getAbbreviate = input.parse("May 22 2020 4:22 AM");    // 解析输入
    Log.i("ouput_Date", "onCreate:" + output.format(getAbbreviate));    // 格式化输出
    String date = output.format(getAbbreviate);
} catch (ParseException e) {
    e.printStackTrace();
}
英文:

Please change your SimpleDateFormat

Try This:

 SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date getAbbreviate=input.parse("May 22 2020 4:22 AM");    // parse input
        Log.i("ouput_Date", "onCreate: "+output.format(getAbbreviate));    // format output
        String date=output.format(getAbbreviate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

huangapple
  • 本文由 发表于 2020年4月8日 12:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61093361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定