简体   繁体   English

以dd-MM-yyyy格式发行

[英]issue in the dd-MM-yyyy format

I am trying to parse date in dd-MM-yyyy and my input is 01-01-2017 it is working fine but when i enter 1-1-2017 or 1-1-17 it is also working but i need only in the format of 01-01-2017 please can any one provide me solution 我正在尝试在dd-MM-yyyy中解析日期,我的输入是01-01-2017,它工作正常,但是当我输入1-1-2017或1-1-17时,它也工作,但是我只需要在2017年1月1日的格式,任何人都可以向我提供解决方案

Thanks in advance 提前致谢

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setLenient(false);
        String dateInString = "1-01-2013";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

SimpleDateFormat , even with lenient to false (that is the more strict mode) is not enough to get a full strict parsing. SimpleDateFormat ,即使lenientfalse (即更严格的模式)也不足以获得完整的严格解析。

With DateTimeFormatter Java 8, you could achieve it : 使用DateTimeFormatter Java 8,您可以实现它:

String dateInString = "1-01-2013";

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
try {
    LocalDate date = LocalDate.parse(dateInString, dtf);
    ...
} catch (java.time.format.DateTimeParseException e) {
    ...
}

For information, ofPattern(String pattern) uses by default a ResolverStyle with the java.time.format.ResolverStyle.SMART value. 对于信息, ofPattern(String pattern)默认使用具有java.time.format.ResolverStyle.SMART值的ResolverStyle

In addition to davidxxx's solution, you can also use regex to check :) 除了davidxxx的解决方案之外,您还可以使用正则表达式检查:)

Pattern p =  Pattern.compile("\\d{2}-\\d{2}-\\d{4}");
if (!p.matcher(dateInString).matches()) {
    // bad date format!
}

Still, I do not understand why you are being so strict. 不过,我不明白您为什么这么严格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java (JSP) 中获取日期的 dd-MM-yyyy 格式 - Get the dd-MM-yyyy format of date in Java (JSP) 将日历日期格式设置为受时区影响的dd-MM-yyyy - Format calendar date to dd-MM-yyyy affected by timezone 如何将Datepicker格式转换为dd-MM-yyyy - How to convert datepicker format to dd-MM-yyyy 格式字符串日期为dd-MM-YYYY(不可解析的日期) - Format String Date from dd-MM-YYYY (Unparseable date) 将Java中的日期格式从ddMMyyyy更改为dd-MM-yyyy - Change date format in java from ddMMyyyy to dd-MM-yyyy 如何防止FastDateFormat模式“yyyy-MM-dd”以“dd-MM-yyyy”格式解析String - How to prevent FastDateFormat pattern “yyyy-MM-dd” from parsing String in format “dd-MM-yyyy” 我需要将日期格式从 dd-MM-yyyy 更改为 yyyy-MM-DD - I need to change date format from dd-MM-yyyy to yyyy-MM-DD 如何以'dd-mm-yyyy'和'yyyy-yyyy'格式SWING在JComboBox中放置日期列表? - How to put list of dates in JComboBox in 'dd-mm-yyyy' and also in 'yyyy-yyyy' format SWING? 将格式为“ dd-MM-yyyy HH:mm:ss”的日期字段索引到ElasticSearch 2.2.1中 - Indexing date fields with format “dd-MM-yyyy HH:mm:ss” into ElasticSearch 2.2.1 java.lang.IllegalArgumentException:格式无效:“dd-MM-yyyy HH:mm:ss” - java.lang.IllegalArgumentException: Invalid format: “dd-MM-yyyy HH:mm:ss”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM