尝试将字符串日期转换为日期 [英] Trying to convert string date into a date

查看:49
本文介绍了尝试将字符串日期转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.text.ParseException:无法解析的日期:"2010年5月1日美国夏令时00:00:00"

java.text.ParseException: Unparseable date: "Sat May 01 00:00:00 EDT 2010"

我正在尝试使用SimpleDateFormat类解析此日期.

I am trying to parse this date using the SimpleDateFormat class.

java.util.Date prevStartDate = new Date();
java.util.Date prevStopDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
prevStartDate = dateFormat.format(startDateLY);

推荐答案

那是因为您使用的是 yyyy-MM-dd 的格式-您必须在输入中添加每个参数格式.

That would be because you're using the format of yyyy-MM-dd - you have to add each parameter in your input to that format.

您的格式似乎为 E MMM dd HH:mm:ss z yyyy

因此您需要从一种转换为另一种:

So you need to convert from one to the other:

static DateFormat extended = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
static DateFormat simple = new SimpleDateFormat("yyyy-MM-dd");

String reformat(String extendedString) {
    Date yourDate = extended.parse(extendedString);
    String simpleString = simple.format(yourDate);
    return simpleString;
}

或者,

String reformat(String dateString) {
    return simple.format(extended.parse(dateString));
}

这篇关于尝试将字符串日期转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆