Java 8: How to parse expiration date of debit card? - java

It is really easy to parse expiration date of debit/credit card with Joda time:
org.joda.time.format.DateTimeFormatter dateTimeFormatter = org.joda.time.format.DateTimeFormat.forPattern("MMyy").withZone(DateTimeZone.forID("UTC"));
org.joda.time.DateTime jodaDateTime = dateTimeFormatter.parseDateTime("0216");
System.out.println(jodaDateTime);
Out: 2016-02-01T00:00:00.000Z
I tried to do the same but with Java Time API:
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("MMyy").withZone(ZoneId.of("UTC"));
java.time.LocalDate localDate = java.time.LocalDate.parse("0216", formatter);
System.out.println(localDate);
Output:
Caused by: java.time.DateTimeException: Unable to obtain LocalDate
from TemporalAccessor: {MonthOfYear=2, Year=2016},ISO,UTC of type
java.time.format.Parsed at
java.time.LocalDate.from(LocalDate.java:368) at
java.time.format.Parsed.query(Parsed.java:226) at
java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 30 more
Where I made a mistake and how to resolve it?

A LocalDate represents date that is composed of a year, a month and a day. You can't make a LocalDate if you don't have those three fields defined. In this case, you are parsing a month and a year, but there is no day. As such, you can't parse it in a LocalDate.
If the day is irrelevant, you could parse it into a YearMonth object:
YearMonth is an immutable date-time object that represents the combination of a year and month.
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMyy").withZone(ZoneId.of("UTC"));
YearMonth yearMonth = YearMonth.parse("0216", formatter);
System.out.println(yearMonth); // prints "2016-02"
}
You could then transform this YearMonth into a LocalDate by adjusting it to the first day of the month for example:
LocalDate localDate = yearMonth.atDay(1);

Related

How can I parse a month and a year without a date in Java?

I tried it like this
public LocalDate parseDate(String date) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern("MM-yyyy"));
}
but this code throw an exception
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=5, Year=2022},ISO of type java.time.format.Parsed
YearMonth
You cannot create a LocalDate having a month of year and the year only, it just needs a day of month (and does not provide any default value).
Since you are trying to parse a String of the format "MM-uuuu", I assume you are not interested in creating a LocalDate, which inevitably boils down to the use of a java.time.YearMonth.
Example:
public static void main(String[] args) {
// an arbitrary mont of year
String strMay2022 = "05-2022";
// prepare the formatter in order to parse it
DateTimeFormatter ymDtf = DateTimeFormatter.ofPattern("MM-uuuu");
// then parse it to a YearMonth
YearMonth may2022 = YearMonth.parse(strMay2022, ymDtf);
// if necessary, define the day of that YearMonth to get a LocalDate
LocalDate may1st2022 = may2022.atDay(1);
// print something meaningful concerning the topic…
System.out.println(may1st2022 + " is the first day of " + may2022);
}
Output:
2022-05-01 is the first day of 2022-05

LocalDate: parse MM-yyyy

I get java.time.format.DateTimeParseException after attempt to do following:
LocalDate.parse( "09-2017" , DateTimeFormatter.ofPattern("MM-yyyy") )
What is wrong? Is there any utility in Java to check dateString formats?
A LocalDate needs the day, month and year to be built. Your input has only the month and year. You'll have to choose an arbitrary day and set it to the parsed object to create a LocalDate.
You can either parse it to a java.time.YearMonth, and then choose the day:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM-yyyy");
YearMonth ym = YearMonth.parse("09-2017", fmt);
LocalDate dt = ym.atDay(1); // choose whatever day you want
Or you can use a java.time.format.DateTimeFormatterBuilder with a java.time.temporal.ChronoField to define a default value for the day:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// month-year
.appendPattern("MM-yyyy")
// default value for day
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
// create formatter
.toFormatter();
LocalDate dt = LocalDate.parse("09-2017", fmt);
PS: If you just want to check if the input is correct, just parsing it to a YearMonth is enough (it already checks if the parsed values are valid).

Java: Unable to obtain LocalDate from TemporalAccessor

I am trying to change the format of a String date from EEEE MMMM d to MM/d/yyyy by, first, converting it into a LocalDate and then applying a formatter of a different pattern to the LocalDate before parsing it into String again.
Here's my code:
private String convertDate(String stringDate)
{
//from EEEE MMMM d -> MM/dd/yyyy
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
.toFormatter();
LocalDate parsedDate = LocalDate.parse(stringDate, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String formattedStringDate = parsedDate.format(formatter2);
return formattedStringDate;
}
However, I get this exception message that I don't really understand:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'TUESDAY JULY 25' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=2, MonthOfYear=7, DayOfMonth=25},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
As the other answers already said, to create a LocalDate you need the year, which is not in the input String. It has only day, month and day of the week.
To get the full LocalDate, you need to parse the day and month and find a year in which this day/month combination matches the day of the week.
Of course you could ignore the day of the week and assume that the date is always in the current year; in this case, the other answers already provided the solution. But if you want to find the year that exactly matches the day of the week, you must loop until you find it.
I'm also creating a formatter with a java.util.Locale, to make it explicit that I want month and day of week names in English. If you don't specify a locale, it uses the system's default, and it's not guaranteed to always be English (and it can be changed without notice, even at runtime).
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
// use English Locale to correctly parse month and day of week
.toFormatter(Locale.ENGLISH);
// parse input
TemporalAccessor parsed = formatter.parse("TUESDAY JULY 25");
// get month and day
MonthDay md = MonthDay.from(parsed);
// get day of week
DayOfWeek dow = DayOfWeek.from(parsed);
LocalDate date;
// start with some arbitrary year, stop at some arbitrary value
for(int year = 2017; year > 1970; year--) {
// get day and month at the year
date = md.atYear(year);
// check if the day of week is the same
if (date.getDayOfWeek() == dow) {
// found: 'date' is the correct LocalDate
break;
}
}
In this example, I started at year 2017 and tried to find a date until back to 1970, but you can adapt to the values that fits your use cases.
You can also get the current year (instead of some fixed arbitrary value) by using Year.now().getValue().
The documentation for LocalDate says, that
LocalDate is an immutable date-time object that represents a date,
often viewed as year-month-day. For example, the value "2nd October
2007" can be stored in a LocalDate.
In your case, the input String is missing an important component of LocalDate , i.e the year. What you have basically is month and day. So, you can use a class suited to that MonthDay. Using that your code can be modified to :
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
.toFormatter();
MonthDay monthDay = MonthDay.parse(stringDate, formatter);
LocalDate parsedDate = monthDay.atYear(2017); // or whatever year you want it at
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String formattedStringDate = parsedDate.format(formatter2);
System.out.println(formattedStringDate); //For "TUESDAY JULY 25" input, it gives the output 07/25/2017
Here is the minor change which you need to implement:
private static String convertDate(String stringDate)
{
//from EEEE MMMM d -> MM/dd/yyyy
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("EEEE MMMM dd")
.parseDefaulting(ChronoField.YEAR, 2017)
.toFormatter();
LocalDate parsedDate = LocalDate.parse(stringDate, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String formattedStringDate = parsedDate.format(formatter2);
return formattedStringDate;
}
Add the default chronological year in the formatter using .parseDefaulting(ChronoField.YEAR, 2017)
Call the method using the argument "Tuesday July 25" like this convertDate("Tuesday July 25");
Another option is to do the following (just like the other answers a bit hacky), assuming of course you want the date to fall in the current year:
LocalDate localDate = LocalDate.parse(stringDate + " " +LocalDate.now().getYear(), DateTimeFormatter.ofPattern("EEEE MMMM d");

convert shortdate to LocalDate

Hi i have a short date format with me in the pattern E dd/MM , Is there any way i can convert it to LocalDate.
String date = "Thu 07/05";
String formatter = "E dd/MM";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
final LocalDate localDate = LocalDate.parse(date, formatter);`
But it throws an exception java.time.format.DateTimeParseException: Text 'Thu 07/05' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=5, DayOfMonth=7, DayOfWeek=4},ISO of type java.time.format.Parsed
Is there any way we can fix this issue ?
All you have is a month and a day - so you can create a MonthDay (to create a LocalDate you would also need a year):
MonthDay md = MonthDay.parse(date, formatter);
If you want a LocalDate, you can use the MonthDay as a starting point:
int year = Year.now().getValue();
LocalDate localDate = md.atYear(year);
Or alternatively you can use a default year in your formatter:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern(pattern)
.parseDefaulting(ChronoField.YEAR, year)
.toFormatter(Locale.US);
LocalDate localDate = LocalDate.parse(date, formatter);
The benefit of this method is that it will also check that the day of week (Thursday) is correct.

Why my pattern("yyyyMM") cannot parse with DateTimeFormatter (java 8)

When I using SimpleDateFormat, it can parse.
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
format.setLenient(false);
Date d = format.parse(date);
But When I use Java 8 DateTimeFormatter,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
LocalDate localDate = LocalDate.parse(date, formatter);
it throws
java.time.format.DateTimeParseException: Text '201510' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2015, MonthOfYear=10},ISO of type java
.time.format.Parsed
String value for date is "201510".
Ask yourself the question: which day should be parsed with the String "201510"? A LocalDate needs a day but since there is no day in the date to parse, an instance of LocalDate can't be constructed.
If you just want to parse a year and a month, you can use the YearMonth object instead:
YearMonth localDate = YearMonth.parse(date, formatter);
However, if you really want to have a LocalDate to be parsed from this String, you can build your own DateTimeFormatter so that it uses the first day of the month as default value:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyyMM")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.toFormatter();
LocalDate localDate = LocalDate.parse(date, formatter);
You can use a YearMonth and specify the day you want (say the first for example):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
LocalDate localDate = YearMonth.parse(date, formatter).atDay(1);
Or if the day is irrelevant, just use a YearMonth.

Categories