- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 8 重新格式化今天的日期,但出现以下错误:
java.time.format.DateTimeParseException: Text '09-OCT-2017' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2017, MonthOfYear=10, DayOfYear=9},ISO of type java.time.format.Parsed
代码:
public static String formatDate(String inputDate, String inputDateFormat, String returnDateFormat){
try {
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern(inputDateFormat).toFormatter(Locale.ENGLISH);
LocalDate localDate = LocalDate.parse(inputDate, inputFormatter);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(returnDateFormat);
String formattedString = localDate.format(outputFormatter);
return formattedString;
} catch (DateTimeParseException dtpe) {
log.error("A DateTimeParseException exception occured parsing the inputDate : " + inputDate + " and converting it to a " + returnDateFormat + " format. Exception is : " + dtpe);
}
return null;
}
我以前尝试过使用 SimpleDateFormat
,但问题是我的 inputDateFormat
格式总是大写 DD-MMM-YYYY
,这给了我的结果不正确,所以我尝试使用 parseCaseInsensitive()
来忽略区分大小写。
最佳答案
In the comments你告诉输入格式是 DD-MMM-YYYY
。 According to javadoc ,大写的 DD
是 day of year 字段,YYYY
是 week based year 字段(可能是different from the year field ).
您需要将它们更改为小写的 dd
(月份的日期)和 yyyy
(时代的年份 ). parseCaseInsensitive()
只处理 text 字段 - 在这种情况下,月份名称(数字不受区分大小写的影响 - 只是因为月份是大写的,这并不意味着数字模式也应该是)。
其余的代码是正确的。示例(将格式更改为 yyyyMMdd
):
String inputDate = "09-OCT-2017";
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
// use "dd" for day of month and "yyyy" for year
.appendPattern("dd-MMM-yyyy")
.toFormatter(Locale.ENGLISH);
LocalDate localDate = LocalDate.parse(inputDate, inputFormatter);
// use "dd" for day of month and "yyyy" for year
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedString = localDate.format(outputFormatter);
System.out.println(formattedString); // 20171009
上面代码的输出是:
20171009
关于 your other comment关于无法控制输入模式,一种替代方法是手动将字母替换为小写版本:
String pattern = "DD-MMM-YYYY";
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
// replace DD and YYYY with the lowercase versions
.appendPattern(pattern.replace("DD", "dd").replaceAll("YYYY", "yyyy"))
.toFormatter(Locale.ENGLISH);
// do the same for output format if needed
我不认为它需要一个complex-replace-everything-in-one-step 正则表达式。只需多次调用 replace
方法就可以解决问题(除非您有真的复杂的模式,需要多次不同且复杂的 replace
调用,但仅使用您提供的案例就足够了)。
关于java - 将日期从 DD-MMM-YYYY 重新格式化为 YYYYDDMM 或 YYYYMMDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645073/
我需要使用正则表达式验证格式为 dd/mm/yyyy 的日期字符串。 此正则表达式验证 dd/mm/yyyy,但不验证 31/02/4500 等无效日期: ^(0?[1-9]|[12][0-9]|3[
我需要检查给定字符串的日期。我得到的字符串不规则,我不想使用 Dateformat 或类似的东西。我试图避免出现多个异常。我需要 dd.MM.yyyy 和 MM.yyyy 和 yyyy 的正则表达式。
我想使用在这里找到的 jQuery 屏蔽输入插件 http://digitalbush.com/projects/masked-input-plugin/格式化日期。 我正在从数据库中提取数据以在页面
我正在尝试以更有效的方式根据当前日期找出当前财政年度(FY - 3 月至 4 月)。这是我到目前为止写的内容 public static void main(String[] args) {
我已经搜索了该网站,但没有找到接近我需要的答案。基本上我的日期正确显示为“2013-05-09”,但我想要的是“2013 年 3 月 19 日”或使其更好“3 月 19 日, 2013 年”。 请向我
我需要使用 C# 更改日期字符串的格式 来自:“06/16/2010”或“16/06/2010” 至:“2010-06-16” 你能帮我实现这个吗 谢谢 最佳答案 如果您已经将它作为 DateTime
我正在以 MMM-yyyy 格式从 View 中提交日期值,即“Apr-2019”作为字符串。我需要将字符串值转换为 .Net 支持的日期格式,如 dd-MM-yyyy 或 yyyy-MM-dd。这里
我有一个包含以下数据的平面文件 2/12/2016 2/3/2017 12/23/2017 04/23/2017 第一个是 M/dd/yyyy,第二个是 M/d/yyyy,其余日期格式为 MM/dd/
好吧,我已经和这个战斗了足够长的时间。我有一个正则表达式 ^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d) 这将匹配 8/15、08/15
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Extending JavaScript's Date.parse to allow for DD/MM/Y
我有 json 对象列表,其中一个字段是日期。问题是日期在 json 中以不同的方式写入。 大多数看起来像: "publishedDate": "2005-01-28" "publishedDat
这个问题已经有答案了: Generate & parse "Year-Month" values in text from Java (1 个回答) 已关闭 7 年前。 我有一个变量: YearMon
我对日期格式有一个奇怪的问题。就像,我正在使用 jquery 日期选择器,它设置为格式 dd/mm/yy。因此,当我在该字段中填写 01/09/2015 时,它应该意味着日期是 2015 年 9 月
我有以下日期, 24/04/2019 13/05/2019 12/04/2019 我想删除年份并将这些日期转换为以下格式, 24/04 13/05 12/04 这不是仅用于输出我想在该日期执行加法或申
“YYYY”和“yyyy”之间的确切区别是什么。我读过 this link ,它指出 A common mistake is to use YYYY. yyyy specifies the calen
我有一个日期列,其中日期格式为 2010-04-14 在 SQL Server 中。有没有办法将日期格式检索为 14/04/2010 在选择语句中? 最佳答案 试试这个: SELECT CONVERT
我有这个正则表达式:(\d{4})\/(\d{2,4})\/?(\w{2})?这似乎工作正常,但我想避免以下情况,例如:我有一个字符串“2020/2000/WEEEE”并且它匹配它。 它匹配的其他错误
这是用户输入日期的代码:(它必须是我的日期选择器,但表单必须将日期字段作为文本提交 - 不要问) 提交时,我在 javascript 中调用验证逻辑。我附上了当我尝试输入 01/01/2001 作为用
我正在尝试以 YYYY-YY 格式提取会计日期字符串的最后一年 (YY)。例如,这个“1999-00”的最后一年是 2000 年。 当前代码似乎涵盖了除此之外的大多数情况。 import pandas
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Conversion of Date 我从对话框中获取的日期为:5-1-2012 - 作为字符串。 我需要将其转换为
我是一名优秀的程序员,十分优秀!