Java date format for 00 days / months

I have a date like String, but sometimes it does not have a valid day or month (values ​​are zeros, 00).

Example:

String value = "03082017";

Then I do:

    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;

This works as an example String.

Now I have this line:

String value = "00092017" //ddMMyyyy 

Then my code converts the day 00to 01.

This works for the template ddMMyyyy, but now this is my problem: I may have different templates: ddMMyyyyeither MMddyyyyor yyyy/dd/MMetc.

My solution is to check the pattern first (e.g. MMddyyyy), and then look at your value 03092017( ddMMyyyy) and take the numbers in my daily line, where is the dd position from my pattern.

, 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
}
+4
1

Java 8, java.time API. , , API.

Java <= 7, ThreeTen Backport, backport Java 8 . Android ThreeTenABP ( , ).

. - ( 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, 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. , , , :

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
+3

Source: https://habr.com/ru/post/1685078/


All Articles