Parsing date without month using DateTimeFormatter

Happy

I try to parse a date with this format: ddYYYY. For example, I have the string 141968, and I want to know that day = 14 and year = 1968.

I suppose I have to use directly a TemporalAccessor gave by DateTimeFormatter.parse(String), but I cannot find how to use this result. While debugging I see the result is a java.time.Parsed which is not public but contains informations I want in field fieldValues.

How can I parse this particular format?

Thank you.

JodaStephen

One approach is to default the missing month field:

DateTimeFormatter f = new DateTimeFormatterBuilder()
  .appendPattern("ddyyyy")
  .parseDefaulting(MONTH_OF_YEAR, 1)
  .toFormatter();
LocalDate date = LocalDate.parse("141968", f);
System.out.println(date.getDayOfMonth());
System.out.println(date.getYear());

Another is to query the TemporalAccessor:

DateTimeFormatter f = DateTimeFormatter.ofPattern("ddyyyy");
TemporalAccessor parsed = f.parse("141968");
System.out.println(parsed.get(ChronoField.YEAR));
System.out.println(parsed.get(ChronoField.DAY_OF_MONTH));

(Note the use of "y", not "Y" for parsing)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing a date using DateTimeFormatter ofPattern

From Dev

Using new Java 8 DateTimeFormatter to do strict date parsing

From Dev

Parsing with Java 8 DateTimeFormatter and Spanish month names

From Dev

Date not parsing the month

From Dev

parsing date from day of week with java datetimeformatter

From Dev

Parsing an "integer" time with DateTimeFormatter using optional sections

From Dev

Android: Parsing datetime with timezone using DateTimeFormatter

From Dev

Parsing date and time with the new java.time.DateTimeFormatter

From Dev

Strict parsing date time in groovy without using static objects

From Dev

Android date parsing (Extracting month and year )

From Dev

Date Parser parsing every month to January

From Dev

Date parsing of character vector ("Month-Year")

From Dev

parsing dates with month without leading 0

From Dev

Parsing date using strftime

From Dev

ThreeTenABP: How to validate date using a custom date format / DateTimeFormatter?

From Dev

How to print a date using DateTimeFormatter.ISO_LOCAL_DATE?

From Java

Java DateTimeFormatter is not parsing as expected

From Dev

get date of 3rd friday in the month with SQL without using level function

From Dev

Runtime exception while trying to parse date time using joda DateTimeFormatter

From Dev

Pikaday : Modify month or year without click a date

From Dev

Excel date format with abbreviated month without the dot

From Dev

Date Formatting using only date and month?

From Dev

How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?

From Dev

Date parsing using regular expression

From Dev

Parsing without duplicates using XSLT

From Dev

Wrong month number when parsing datetime string with date filter

From Dev

Local date parsing not working without separators

From Dev

How to test parsing a date with SimpleDateFormat without ParseException

From Dev

Logstash date parsing as timestamp using the date filter

Related Related

  1. 1

    Parsing a date using DateTimeFormatter ofPattern

  2. 2

    Using new Java 8 DateTimeFormatter to do strict date parsing

  3. 3

    Parsing with Java 8 DateTimeFormatter and Spanish month names

  4. 4

    Date not parsing the month

  5. 5

    parsing date from day of week with java datetimeformatter

  6. 6

    Parsing an "integer" time with DateTimeFormatter using optional sections

  7. 7

    Android: Parsing datetime with timezone using DateTimeFormatter

  8. 8

    Parsing date and time with the new java.time.DateTimeFormatter

  9. 9

    Strict parsing date time in groovy without using static objects

  10. 10

    Android date parsing (Extracting month and year )

  11. 11

    Date Parser parsing every month to January

  12. 12

    Date parsing of character vector ("Month-Year")

  13. 13

    parsing dates with month without leading 0

  14. 14

    Parsing date using strftime

  15. 15

    ThreeTenABP: How to validate date using a custom date format / DateTimeFormatter?

  16. 16

    How to print a date using DateTimeFormatter.ISO_LOCAL_DATE?

  17. 17

    Java DateTimeFormatter is not parsing as expected

  18. 18

    get date of 3rd friday in the month with SQL without using level function

  19. 19

    Runtime exception while trying to parse date time using joda DateTimeFormatter

  20. 20

    Pikaday : Modify month or year without click a date

  21. 21

    Excel date format with abbreviated month without the dot

  22. 22

    Date Formatting using only date and month?

  23. 23

    How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?

  24. 24

    Date parsing using regular expression

  25. 25

    Parsing without duplicates using XSLT

  26. 26

    Wrong month number when parsing datetime string with date filter

  27. 27

    Local date parsing not working without separators

  28. 28

    How to test parsing a date with SimpleDateFormat without ParseException

  29. 29

    Logstash date parsing as timestamp using the date filter

HotTag

Archive