00天/月的Java日期格式

00天/月的Java日期格式,java,date,zero,date-parsing,dayofmonth,Java,Date,Zero,Date Parsing,Dayofmonth,我的日期为String,但有时它没有有效的日期或月份(值为零,00) 例如: String value = "03082017"; 那么我会: String day = value.substring(0,2); String month = value.substring(2,4); String year = value.substring(4,8); if(day.equals("00")) { day = "01"; }

我的日期为
String
,但有时它没有有效的日期或月份(值为零,
00

例如:

String value = "03082017";
那么我会:

    String day = value.substring(0,2);
    String month = value.substring(2,4);
    String year = value.substring(4,8);

    if(day.equals("00")) {
        day = "01";
    }
    if(month.equals("00")) {
        month = "01";
    }

    value = day + month + year;
这适用于示例
字符串

现在我有了这个字符串:

String value = "00092017" //ddMMyyyy 
然后我的代码将
00
天转换为
01

这适用于模式
ddMMyyyy
,但现在是我的问题:我可以有不同的模式:
ddMMyyyy
MMddyyyy
yyyy/dd/MM
,等等

我的解决方案是首先检查模式(例如
MMddyyyy
),然后查看我的值
03092017
ddMMyyyy
),并将数字带到我的日期字符串,该字符串位于我的模式的dd位置

因此,代码具有模式
ddMMyyyy
,但值为
03092017
mmddyyy

我的代码:

public void doSomething(String valueWithDate, String pattern){
    // now I become: 
    // valueWithDate = 01092017
    // pattern = ddMMyyyy

    String day = valueWithDate.substring(0,2);
    String month = valueWithDate.substring(2,4);
    String year = valueWithDate.substring(4,8);

    //Works I get my information but what is when I get other pattern/value? for example:
    // valueWithDate = 09082017
    // pattern = MMddyyyy
    // now I want my information again, but day is not on substring 0,2
}

如果您使用的是Java8,则可以使用。这更容易


如果您使用的是Java,为什么不使用dateformatter呢?如何检查模式?任何小于或等于12的日和月都是有效的,但可能是错误的……除非这是出于教育目的:不要重新发明轮子。Java有大量的选项来进行日期解析。。。当你限制自己使用Java8时,你会得到很多非常有用的东西,这听起来是不可能的。您如何决定11032017是3月11日还是11月3日?在11002017年,您如何决定是否缺少月的某一天或某个月?最好理解一下:我的方法获取两个字符串:值和模式,当值为00082017//ddMMyyyy且模式为ddMMyyyy时,在我的方法中,我有3个字符串:day=month=和year=now,我将填充字符串day=00 month=08 year=2017我该怎么做?我在第一篇文章中的例子是有效的,但当我得到值09002017和模式mmddyyy时,它就不起作用了。在严格模式下允许零天或零月似乎是不合逻辑的。真奇怪。要了解更优雅的方式,请看这个。@MenoHochschild事实上,我还认为严格应该意味着我不接受奇怪的值,但它确实有效。坏名字的选择,也许?在Java-8的范围内,我建议您考虑替换<代码>语法分析器。PARSEUNDROBE(“00092017”,新PARSESTATH(0))因为它似乎不依赖于解决风格的非理性行为。@ MeNoHooChsILDD很棒, PARSEUNDENECTION/COD> >不管解析风格如何。我已经更新了答案,非常感谢!谢谢你的编辑。我认为在大多数用例中,在
parseUnresolved()
之后,我会检查parse位置是否指示整个字符串已被解析。这将加强输入验证。
public void doSomething(String valueWithDate, String pattern){
    // now I become: 
    // valueWithDate = 01092017
    // pattern = ddMMyyyy

    String day = valueWithDate.substring(0,2);
    String month = valueWithDate.substring(2,4);
    String year = valueWithDate.substring(4,8);

    //Works I get my information but what is when I get other pattern/value? for example:
    // valueWithDate = 09082017
    // pattern = MMddyyyy
    // now I want my information again, but day is not on substring 0,2
}
String valueWithDate = "00092017";
String pattern = "ddMMyyyy";
// create formatter with the pattern and strict mode
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.STRICT);
// parse the input
TemporalAccessor parsed = fmt.parse(valueWithDate);

// get day from the parsed object
int day = (int) parsed.getLong(ChronoField.DAY_OF_MONTH);
if (day == 0) {
    day = 1;
}
// get month from the parsed object
int month = (int) parsed.getLong(ChronoField.MONTH_OF_YEAR);
if (month == 0) {
    month = 1;
}
// get year from the parsed object
int year = (int) parsed.getLong(ChronoField.YEAR_OF_ERA);

// the final value will have the same pattern? if so, just use the same formatter
String value = fmt.format(LocalDate.of(year, month, day));
System.out.println(value); // 01092017
// create formatter, no need of strict mode
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
// parse
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = fmt.parseUnresolved(valueWithDate, pos);
// check errors
if (pos.getErrorIndex() >= 0) {
    // error in parsing (getErrorIndex() returns the index where the error occurred)
}
// rest of the code is the same
if(pos.getIndex() < valueWithDate.length()) {
    // the input String wasn't fully parsed
}
// output in another format
DateTimeFormatter output = DateTimeFormatter.ofPattern("MMddyyyy");
String value = output.format(LocalDate.of(year, month, day));
System.out.println(value); // 09012017