Java 00 的日期格式 days/months

Java date format for 00 days/months

我有一个日期 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

现在我有了这个字符串:

String value = "00092017" //ddMMyyyy 

然后我的代码将 00 天转换为 01

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

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

因此代码具有模式 ddMMyyyy 但值为 03092017 (MMddyyyy)

String day = "03";
.....

我的代码:

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
}

如果您使用 Java 8,则可以使用 new java.time API. It's easier, less bugged and less error-prone than the old APIs.

如果您使用 Java <= 7,您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it ).

下面的代码适用于两者。 唯一的区别是包名称(在 Java 8 中是 java.time,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp),但是 类和方法名称相同。

您可以使用具有严格模式的 DateTimeFormatter 来做到这一点。这允许日和月中的值为零。其他模式不起作用:默认模式不接受零,宽松模式尝试进行自动转换,因此严格模式是唯一适用于这种情况的模式。

然后您获取各个字段(日和月)并检查它们是否为零。我正在使用 getLong 方法(并转换为 int),因为 get 方法(即 returns 和 int)检查是否返回值是有效的(所以像零这样的值会抛出异常)。

最后,你加入所有的部分得到输出:

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

因此,对于输入 00092017 和模式 ddMMyyyy,上面的代码将打印:

01092017


正如 所建议的,您还可以使用 parseUnresolved 方法,该方法不验证值,因此它允许日和月为零(并且您不需要将格式化程序设置为严格模式)。

此方法还需要一个 java.text.ParsePosition,用于检查解析错误(因为它不会像 parse 方法那样抛出异常):

// 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