不同格式的Java日期
嘿,我用下面的代码来解析不同格式的日期 trubel是,它采用第一种格式并解析日期,即使它不适合不同格式的Java日期,java,date,Java,Date,嘿,我用下面的代码来解析不同格式的日期 trubel是,它采用第一种格式并解析日期,即使它不适合 public static Date parseDate(String date) { date = date.replaceAll("[-,.;:_/&()+*# ']", "-"); System.out.print(date); List<String> formatStrings = Arrays.asList("d-M-y","y-M-d");
public static Date parseDate(String date) {
date = date.replaceAll("[-,.;:_/&()+*# ']", "-");
System.out.print(date);
List<String> formatStrings = Arrays.asList("d-M-y","y-M-d");
for (String formatString : formatStrings) {
try {
System.out.println(new SimpleDateFormat(formatString).parse(date));
return new SimpleDateFormat(formatString).parse(date);
} catch (Exception e) {
}
}
return null;
}
或
所以我不明白为什么第一种格式总是解析?您的循环在第一个
return
语句处停止,从循环中删除return语句,然后它将删除所有日期格式
编辑:根据评论中的要求:
public static Date parseDate(String date) {
Date dateToReturn = null;
date = date.replaceAll("[,\\.;:_/&\\(\\)\\+\\*# ']", "-");
String formatString = "d-M-y";
if (date.matches("\\d{4}-\\d{2}-\\d{2}")) {
formatString = "y-M-d";
}
try {
dateToReturn = new SimpleDateFormat(formatString).parse(date);
} catch (Exception e) {
System.out.println("the given date does not math this format " + formatString);
}
return dateToReturn;
}
在上面,我认为您只有两种可能的格式。您的循环在第一个
return
语句处停止,从循环中删除return语句,然后它将删除所有日期格式
编辑:根据评论中的要求:
public static Date parseDate(String date) {
Date dateToReturn = null;
date = date.replaceAll("[,\\.;:_/&\\(\\)\\+\\*# ']", "-");
String formatString = "d-M-y";
if (date.matches("\\d{4}-\\d{2}-\\d{2}")) {
formatString = "y-M-d";
}
try {
dateToReturn = new SimpleDateFormat(formatString).parse(date);
} catch (Exception e) {
System.out.println("the given date does not math this format " + formatString);
}
return dateToReturn;
}
在上面,我认为您只有两种可能的格式。这里是jshell中一个更简单的示例。“d-M-y”不解析这两个,因为它默认为lenient。如果执行
setLenient(false)
操作,则会在日期以其他格式显示异常
| Welcome to JShell -- Version 9.0.1
| For an introduction type: /help intro
jshell> java.text.SimpleDateFormat dmy = new java.text.SimpleDateFormat("d-M-y");
dmy ==> java.text.SimpleDateFormat@596ca10
jshell> dmy.parse("13-02-1994")
$2 ==> Sun Feb 13 00:00:00 CST 1994
jshell> dmy.parse("1994-02-13")
$3 ==> Wed Jul 18 00:00:00 CDT 2018
jshell> dmy.isLenient()
$4 ==> true
jshell> dmy.setLenient(false)
jshell> dmy.parse("1994-02-13")
| java.text.ParseException thrown: Unparseable date: "1994-02-13"
| at DateFormat.parse (DateFormat.java:388)
| at (#6:1)
仅供参考,Java 8+API实现此目的的方法:
jshell> java.time.format.DateTimeFormatter dmy = java.time.format.DateTimeFormatter.ofPattern("dd-MM-yyyy");
dmy ==> Value(DayOfMonth,2)'-'Value(MonthOfYear,2)'-'Value(YearOfEra,4,19,EXCEEDS_PAD)
jshell> java.time.LocalDate.parse("13-02-1994", dmy)
$2 ==> 1994-02-13
jshell> java.time.LocalDate.parse("1994-02-13", dmy)
| java.time.format.DateTimeParseException thrown: Text '1994-02-13' could not be parsed at index 2
| at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:1988)
| at DateTimeFormatter.parse (DateTimeFormatter.java:1890)
| at LocalDate.parse (LocalDate.java:428)
| at (#3:1)
下面是jshell中的一个简单示例。“d-M-y”不解析这两个,因为它默认为lenient。如果执行
setLenient(false)
操作,则会在日期以其他格式显示异常
| Welcome to JShell -- Version 9.0.1
| For an introduction type: /help intro
jshell> java.text.SimpleDateFormat dmy = new java.text.SimpleDateFormat("d-M-y");
dmy ==> java.text.SimpleDateFormat@596ca10
jshell> dmy.parse("13-02-1994")
$2 ==> Sun Feb 13 00:00:00 CST 1994
jshell> dmy.parse("1994-02-13")
$3 ==> Wed Jul 18 00:00:00 CDT 2018
jshell> dmy.isLenient()
$4 ==> true
jshell> dmy.setLenient(false)
jshell> dmy.parse("1994-02-13")
| java.text.ParseException thrown: Unparseable date: "1994-02-13"
| at DateFormat.parse (DateFormat.java:388)
| at (#6:1)
仅供参考,Java 8+API实现此目的的方法:
jshell> java.time.format.DateTimeFormatter dmy = java.time.format.DateTimeFormatter.ofPattern("dd-MM-yyyy");
dmy ==> Value(DayOfMonth,2)'-'Value(MonthOfYear,2)'-'Value(YearOfEra,4,19,EXCEEDS_PAD)
jshell> java.time.LocalDate.parse("13-02-1994", dmy)
$2 ==> 1994-02-13
jshell> java.time.LocalDate.parse("1994-02-13", dmy)
| java.time.format.DateTimeParseException thrown: Text '1994-02-13' could not be parsed at index 2
| at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:1988)
| at DateTimeFormatter.parse (DateTimeFormatter.java:1890)
| at LocalDate.parse (LocalDate.java:428)
| at (#3:1)
因为您正在分析字符串。没有格式化它,因为您正在分析字符串。不格式化我想返回一个日期,但如果出现异常,它应该返回?好的,但基于哪种格式,因为我对两种格式感到困惑,所以我在一个web后端中使用了这一格式,并且我可以从browserI编辑的答案中得到不同的格式,我希望它能帮助我返回一个日期,但如果有异常,它应该返回?好的,但基于哪种格式,因为有两种格式让我感到困惑,我在一个web后端中使用了这种格式,而且我可以从browserI编辑的答案中得到不同的格式,我希望它能帮助你这是一个非常好的答案:)我当然推荐Java8方式。谢谢你提供这个。这是一个非常好的答案:)我当然推荐Java8方式。谢谢你提供这个。