Java 使用区域设置将字符串转换为日期
我试图将字符串转换为日期,但每次我这样做时,它总是向我抛出错误,我不确定我遗漏了什么Java 使用区域设置将字符串转换为日期,java,android,datetime,simpledateformat,datetime-parsing,Java,Android,Datetime,Simpledateformat,Datetime Parsing,我试图将字符串转换为日期,但每次我这样做时,它总是向我抛出错误,我不确定我遗漏了什么 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 = forma
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
我错过什么了吗 您可以获得如下时间格式:
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));
您可以获得如下所示的时间格式:
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));
你可以试试这个
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));
你可以试试这个
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));
您正在将日期转换为英语,这导致了异常:
请更改:
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", Locale.ENGLISH);
eventTime.setText(date.toString());
与
简单的日期格式应该是“yyyy-MM-dd hh:MM:ss”来解析它string@JyotiJK谢谢,我想新的SimpleDateFormat(“EEE,MMM d,yy hh:mm a”
是如何解析的-更改为您的方式允许我实际解析日期-但现在我如何将其更改为EEE,MMM d,yy hh:mm a
格式?获取日期后,再次将日期更改为此格式新的SimpleDateFormat(“EEE,MMM d,yy hh:mm a”,Locale.ENGLISH)一个<代码>日期>代码>没有格式,为了获得你想要的格式,你需要用一个新的格式化器把它格式化成一个新的字符串。顺便说一下,扔掉长期过时的和臭名昭著的麻烦代码< SimpleDateFormat <代码>和朋友,然后添加到你的Android PROJ中。ECT,以便使用<代码> java。时间<代码>,现代java日期和时间API。它是非常好的工作。我会认为这是一个非常严重的问题,你不能看到一个STATTACK被打印。你应该看一下修复你的项目设置,这样你就可以看到你的程序试图告诉你发生的错误。日期格式应该是“yyyy-MM-dd hh:MM:ss”来解析它string@JyotiJK谢谢,我想新的SimpleDateFormat(“EEE,MMM d,yy hh:mm a”
是如何解析的-更改为您的方式允许我实际解析日期-但现在我如何将其更改为EEE,MMM d,yy hh:mm a
格式?获取日期后,再次将日期更改为此格式新的SimpleDateFormat(“EEE,MMM d,yy hh:mm a”,Locale.ENGLISH)一个<代码>日期>代码>没有格式,为了获得你想要的格式,你需要用一个新的格式化器把它格式化成一个新的字符串。顺便说一下,扔掉长期过时的和臭名昭著的麻烦代码< SimpleDateFormat <代码>和朋友,然后添加到你的Android PROJ中。ECT,以便使用<代码> java。时间<代码>,现代java日期和时间API。它是非常好的工作。我会认为这是一个非常严重的问题,你不能看到一个STACKTURE被打印。你应该看一下修复你的项目设置,这样你就可以看到你的程序试图告诉你发生的错误。nswer,抱歉。更改后仍会引发相同的异常。抱歉!请将“eventTime.setText(date.toString());”更改为“eventTime.setText(format.format(date));”太好了。Prakar Gupta,如果你认为你对ParseException
的原因有答案,你可以。这比在注释中添加信息要好。回答不正确,对不起。更改后仍然会引发相同的异常。对不起!请将“eventTime.setText(date.toString());”更改为“eventTime.setText(format.format(date));”也是。Prakhar Gupta,如果你认为你对ParseException
的原因有答案,你可以。这比在注释中添加信息要好。