Java 不同的SimpleDataFormat解析
我想检查一个日期是否与我定义的模式匹配。请看下面我的代码Java 不同的SimpleDataFormat解析,java,date,validation,simpledateformat,Java,Date,Validation,Simpledateformat,我想检查一个日期是否与我定义的模式匹配。请看下面我的代码 date = null List<String> formatStrings = Arrays.asList("yyyy-MM-dd", "dd.MM.yyyy", "yyyyMMdd", "yyyy/MM/dd", "dd/MM/yyyy"); for (String formatStr: formatStrings) { try { dateTyp
date = null
List<String> formatStrings = Arrays.asList("yyyy-MM-dd", "dd.MM.yyyy", "yyyyMMdd", "yyyy/MM/dd", "dd/MM/yyyy");
for (String formatStr: formatStrings) {
try {
dateType = new Date(new SimpleDateFormat(formatStr).parse(myCol).getTime());
} catch (ParseException e) {}
}
return date;
我还编写了一个单元测试来测试我的函数是否工作,但它只有在变量有模式时才工作:yyyy-MM-dd,所以是列表中的第一个模式。如果我有任何其他模式,它会显示一个例外
你能帮帮我吗?我写错了哪一行?避免遗留日期时间类
您正在使用可怕的日期时间类,这些类在几年前被JSR310定义的业界领先的现代java.time类所取代
java.time
切换到使用LocalDate和DateTimeFormatter
DateTimeFormatter预定义了两种预期格式,每种格式都是标准格式的变体。对于其他三种格式,我们定义了一种格式化模式
package work.basil.example;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Objects;
public class Demo
{
public static void main ( String[] args )
{
Demo app = new Demo ();
app.demo ();
}
private void demo ( )
{
// ("yyyy-MM-dd", "dd.MM.yyyy", "yyyyMMdd", "yyyy/MM/dd", "dd/MM/yyyy"
final List < DateTimeFormatter > formatterList = List.of (
DateTimeFormatter.ISO_LOCAL_DATE ,
DateTimeFormatter.ofPattern ( "dd.MM.uuuu" ) ,
DateTimeFormatter.BASIC_ISO_DATE ,
DateTimeFormatter.ofPattern ( "uuuu/M/dd" ) ,
DateTimeFormatter.ofPattern ( "dd/MM/uuuu" )
);
final List < String > inputs = List.of ( "2020-01-23" , "23.01.2020" , "20200123" , "2020/01/23" , "23/01/2020" );
for ( String input : inputs )
{
LocalDate localDate = null;
for ( DateTimeFormatter formatter : formatterList )
{
try
{
localDate = LocalDate.parse ( input , formatter );
if ( ! localDate.equals ( LocalDate.of ( 2020 , Month.JANUARY , 23 ) ) )
{
throw new IllegalStateException ( "Oops! Unexpected result. " + input + " ➙ " + localDate );
}
System.out.println ( input + " ➙ " + localDate );
break; // Bail out of this inner FOR loop, as we have successfully parsed this input.
} catch ( DateTimeParseException e )
{
// Swallow exception, as we expect most to fail.
}
}
Objects.requireNonNull ( localDate , "Oops, unexpected input: " + input );
}
}
}
看这个
2020-01-23➙ 2020-01-23
23.01.2020➙ 2020-01-23
20200123➙ 2020-01-23
2020/01/23➙ 2020-01-23
2020年1月23日➙ 2020-01-23
你有什么例外?也许抓住并忽略那个?不,它只是检查我的var是否有第一个模式,但没有其他模式。。。如果它没有第一个,它直接进入例外日期是一个日期。它不能匹配或不匹配模式。日期的字符串表示形式可能与模式匹配。我认为这是正则表达式应用程序的一种tak;在try块内部-否则,即使第一个模式成功,您也会尝试所有模式。仅供参考,您使用的是几年前被JSR 310中定义的java.time类所取代的糟糕的日期时间类。请参阅DateTimeFormatter和DateTimeFormatterBuilder。
package work.basil.example;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Objects;
public class Demo
{
public static void main ( String[] args )
{
Demo app = new Demo ();
app.demo ();
}
private void demo ( )
{
// ("yyyy-MM-dd", "dd.MM.yyyy", "yyyyMMdd", "yyyy/MM/dd", "dd/MM/yyyy"
final List < DateTimeFormatter > formatterList = List.of (
DateTimeFormatter.ISO_LOCAL_DATE ,
DateTimeFormatter.ofPattern ( "dd.MM.uuuu" ) ,
DateTimeFormatter.BASIC_ISO_DATE ,
DateTimeFormatter.ofPattern ( "uuuu/M/dd" ) ,
DateTimeFormatter.ofPattern ( "dd/MM/uuuu" )
);
final List < String > inputs = List.of ( "2020-01-23" , "23.01.2020" , "20200123" , "2020/01/23" , "23/01/2020" );
for ( String input : inputs )
{
LocalDate localDate = null;
for ( DateTimeFormatter formatter : formatterList )
{
try
{
localDate = LocalDate.parse ( input , formatter );
if ( ! localDate.equals ( LocalDate.of ( 2020 , Month.JANUARY , 23 ) ) )
{
throw new IllegalStateException ( "Oops! Unexpected result. " + input + " ➙ " + localDate );
}
System.out.println ( input + " ➙ " + localDate );
break; // Bail out of this inner FOR loop, as we have successfully parsed this input.
} catch ( DateTimeParseException e )
{
// Swallow exception, as we expect most to fail.
}
}
Objects.requireNonNull ( localDate , "Oops, unexpected input: " + input );
}
}
}