00天/月的Java日期格式

标签 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";
    }
    if(month.equals("00")) {
        month = "01";
    }

    value = day + month + year;

这适用于示例 String

现在我有了这个字符串:

String value = "00092017" //ddMMyyyy 

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

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

我的解决方案是先检查模式(例如MMddyyyy),然后查看我的值03092017(ddMMyyyy)并取数字到我的日期字符串,它位于我模式中的 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 .这更容易,less bugged and less error-prone than the old APIs .

如果您使用的是 Java <= 7,您可以使用 ThreeTen Backport ,Java 8 的新日期/时间类的一个很好的反向移植。对于 Android,您还需要 ThreeTenABP (更多关于如何使用它的信息 here)。

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

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

然后您将获得各个字段(日和月)并检查它们是否为零。我正在使用 getLong 方法(并转换为 int),因为 get 方法(返回 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


根据 @MenoHochschild's comment 的建议,您还可以使用 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

作为@OleV.V.'s comment建议,您还可以检查整个输入是否未被解析,使用:

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

关于00天/月的Java日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052505/

相关文章:

java - 我无法修复 "Exception in thread "main"java.lang.ArithmeticException :/by zero"

c# - 将零数字添加到我的十进制值

java - 正则表达式:对匹配组执行操作

java - JDBC 结果集聚合函数

java - Android 日期格式帮助

php - 日期范围的 If 语句

linux - 如何 "zero"为 1 的文件

java - SonarQube pdf 报告创建

java - elasticsearch 2.4 java 1.7连接错误NoNodeAvailableException

javascript - 是否可以使用 CSS 为 JavaScript 生成的 HTML 元素设置样式?