使用语言环境将字符串转换为日期
convert string to date with locale
我正在尝试将字符串转换为日期,但每次我这样做都会向我抛出错误,我不确定我错过了什么
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));
你可以试试这个,
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");
eventTime.setText(format.format(date));
我正在尝试将字符串转换为日期,但每次我这样做都会向我抛出错误,我不确定我错过了什么
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));
你可以试试这个,
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");
eventTime.setText(format.format(date));