问题描述
无论何时我将时间范围更改为英语会话后都以德语会话语言定义。会话(反之亦然)我得到: java.text.ParseException:无法解析的日期:“ 10.10.2018”
以下是片段:
Date startDateFormatted = DateUtils.convertDatetoMinusDayNumber(cal,dayRange);
Date endDateFormatted = new Date();
if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate))
{
try
{
String datePattern = getLocalizedString("dd.MM.yyyy"); //
startDateFormatted = new SimpleDateFormat(datePattern).parse(startDate); // exception is throwing on this line
endDateFormatted = new SimpleDateFormat(datePattern).parse(endDate);
}
catch (final Exception e)
{
LOG.error(ERROR_DATE_PARSING,e);
}
}
解决方法
java.time
我建议您使用现代Java日期和时间API java.time进行日期工作。
from sklearn.svm import SVC
#training on the first model
svm.fit(X_train,y_train)
# predict on the 2nd dataset X2
y_pred = svm.predict(X2)
#evaluate accuracy of predictions for second dataset
print(accuracy_score(Y2,y_pred))
输出:
2018-10-10
如果要为不同的语言环境支持不同的日期格式,请让Java为您处理该部分:
String datePattern = "dd.MM.uuuu";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(datePattern);
String startDateString = "10.10.2018";
LocalDate startDate = LocalDate.parse(startDateString,dateFormatter);
System.out.println(startDate);
德语语言环境可与您的示例字符串 String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.MEDIUM,null,IsoChronology.INSTANCE,Locale.GERMAN);
配合使用。例如,对于英国语言环境,则需要像10.10.2018
这样的字符串,就像英国人通常期望的那样。
您的代码出了什么问题?
我们无法从信息和代码中看出您提供的确切信息。一些不错的猜测是:
- 正如Arvind Kumar Avinash在评论中所说,
10 Oct 2018
可能会造成麻烦。您可以打印getLocalizedString()
进行检查。您可以对显示给用户的字符串进行本地化。尝试本地化格式化程序的格式化模式字符串可能是完全错误的,因此您应该忽略该方法调用。在更改语言时出现错误似乎支持这种可能性。 - 您的字符串中可能有意外的非打印字符。一种检查方法是打印
datePattern
。如果长度大于10,则startDate.length()
中的字符数超过10个字符。
链接
role directory structure解释了如何使用java.time。