How to analyze only four-digit years

I use Joda-Time to analyze these years:

private DateTime attemptParse(String pattern, String date) {
        DateTimeFormatter parser = DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH);
        DateTime parsedDateTime = parser.parseLocalDateTime(date).toDateTime(WET);
        return parsedDateTime;
    }

I'm trying to parse multiple formats "yyyy-MM-dd", "yyyy-MMM-dd","yyyy MMM dd-dd","yyyy MMM", (etc), "yyyy". When someone does not work, I will try the following.

And it works like a charm when the string is really only 4 digits (for example:) "2016". The problem is that sometimes I get things like: "201400". And Joda-Time matches this with the template "yyyy"and returns the date with the year 201400.

I wanted to avoid ugliness by checking if there is any year > 9999. Is there a way to do this using Joda-Time?

+4
source share
2 answers

, DateTimeParser ( , ).

DateTimeFormatterBuilder, ( , , , DateTimeFormat.forPattern()).

org.joda.time.format.DateTimeParser ( ):

// only yyyy
DateTimeParser p1 = new DateTimeFormatterBuilder()
    // year with exactly 4 digits
    .appendYear(4, 4).toParser();
// yyyy-MM-dd
DateTimeParser p2 = new DateTimeFormatterBuilder()
    // year with exactly 4 digits
    .appendYear(4, 4)
    // rest of the pattern
    .appendPattern("-MM-dd").toParser();
// yyyy MMM
DateTimeParser p3 = new DateTimeFormatterBuilder()
    // year with exactly 4 digits
    .appendYear(4, 4)
    // rest of the pattern
    .appendPattern(" MMM").toParser();

DateTimeFormatter:

// create array with all the possible patterns
DateTimeParser[] possiblePatterns = new DateTimeParser[] { p1, p2, p3 };

DateTimeFormatter parser = new DateTimeFormatterBuilder()
    // append all the possible patterns
    .append(null, possiblePatterns)
    // use the locale you want (in case of month names and other locale sensitive data)
    .toFormatter().withLocale(Locale.ENGLISH);

Locale.ENGLISH ( ). , ( MMM , Jan Sep). :

System.out.println(parser.parseLocalDateTime("2014")); // OK
System.out.println(parser.parseLocalDateTime("201400")); // exception
System.out.println(parser.parseLocalDateTime("2014-10-10")); // OK
System.out.println(parser.parseLocalDateTime("201400-10-10")); // exception
System.out.println(parser.parseLocalDateTime("2014 Jul")); // OK
System.out.println(parser.parseLocalDateTime("201400 Jul")); // exception

2014, . 201400, java.lang.IllegalArgumentException, :

java.lang.IllegalArgumentException: : "201400" "00"

DateTimeFormatter , , . (, static final).

, , , , . formatter , , , ( , ).


API / Java

Joda-Time API, . joda : " , Joda-Time " " . Java SE 8, java.time(JSR-310)..

( ) Joda-Time API, .

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

Java 6 7, ThreeTen Backport, Java 8 . Android ThreeTenABP ( , ).

. , ( Java 8 - java.time, ThreeTen Backport ( Android ThreeTenABP) - org.threeten.bp), .

API , , ( , Joda-Time):

// 4 digits in year
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy", Locale.ENGLISH);
fmt.parse("2014"); // OK
fmt.parse("201400"); // exception
fmt.parse("201"); // exception

2014, 201400 201 ( 4 ) :

java.time.format.DateTimeParseException: '201400' 0

.


: , Joda-Time , - (, , 1, // ..).

, . , , , .

, Year :

DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy", Locale.ENGLISH);
System.out.println(Year.parse("2014", parser)); // ok
System.out.println(Year.parse("201400", parser)); // exception

, int:

Year year = Year.parse("2014", parser);
int yearValue = year.getValue(); // 2014

, - API . , DateTimeFormatterBuilder.

LocalDateTime, :

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // string pattern
    .appendPattern("yyyy")
    // default month is January
    .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
    // default day is 1
    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
    // default hour is zero
    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
    // default minute is zero
    .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
    // set locale
    .toFormatter(Locale.ENGLISH);
// create LocalDateTime
System.out.println(LocalDateTime.parse("2014", fmt)); // 2014-01-01T00:00
System.out.println(LocalDateTime.parse("201400", fmt)); // exception

, , .

+3

, , Jodatime - , "201400" 2014 . , . , , :

String normalizedDate = String.format("%4s", date).trim();
0

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


All Articles