0

I want to change the string into the date formate for that I am using SimpleDateFormat class. I am passing the string as String+Integer.toString(int) from list of strings and SimpleDateFormat pattern as an inputs. Note: Instead of String+Integer.toString(int) if I pass actual string like "Jan 09 2019" successfully convert string into the date. I tried a lot with different things.

dateList is a list of "MMM dd" formate dates. Adding year on that formate by doing dateList.get(5)+Integer.toString(year) which is giving me parse exception <<-- Instead of this if I hardcode the date like Jan 09 2019 converting string into the date. finalDatesInMMMDDYYYYFormat is another list where I am saving the dates in MMM dd yyyy format. Utils.parseDate is a method I wrote in Utils class where I mentioned try-catch block.

int year = 2019;
private List<String> dateList = new ArrayList<>();
private List<Date> finalDatesInMMMDDYYYYFormat = new ArrayList<>();
final String testString = dateList.get(5)+Integer.toString(year);
finalDatesInMMMDDYYYYFormat.add(Utils.parseDate(testString, new SimpleDateFormat("MMM dd yyyy")));

Expected: Change the string into the date and add it to finalDatesInMMMDDYYYYFormat

Actual: Getting parse exception.

ferpel
  • 206
  • 3
  • 12
Jigar Patel
  • 21
  • 1
  • 4
  • 1
    Please [edit] your question to include the stack trace for the exception. – azurefrog Feb 11 '19 at 19:21
  • In your format-string is a blank between days and year. In your teststring not. – mayamar Feb 11 '19 at 19:22
  • 1
    You should get an `IndexOutOfBoundsException` trying to access the 6th element of the empty `dateList`. Please [edit] your question and post the actual code. – Robert Feb 11 '19 at 20:05
  • 1
    Can you paste the value of `dateList.get(5)` into your question? – rockfarkas Feb 11 '19 at 21:13
  • Your question is vague. Please either post the whole code or state the values of the variable. In addition the stack trace will be helpful. – Sibgha Feb 11 '19 at 22:13
  • Would it be possible for you to [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please? It would greatly help us spot your bug and answer your question (it may also help yourself do that). – Ole V.V. Feb 12 '19 at 05:28

2 Answers2

1

java.time

    int year = 2019;
    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("MMM dd")
            .toFormatter(Locale.ENGLISH);

    List<LocalDate> finalDatesWithoutFormat = new ArrayList<>();

    String dateString = "JAN 09";
    MonthDay md = MonthDay.parse(dateString, dateFormatter);
    finalDatesWithoutFormat.add(md.atYear(year));

    System.out.println(finalDatesWithoutFormat);

The output from this snippet is:

[2019-01-09]

java.time, the modern Java date and time API, includes a class for a date without year, MonthDay, which may serve your purpose better than an ordinary date. My code also shows how to supply a year to obtain a LocalDate (a date without time of day).

I recommend you don’t use Date and SimpleDateFormat. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome.

What went wrong in your code?

From the information you have provided it’s not possible to tell why your code didn’t work. Possible explanations include the following, but there might be others.

  • As rockfarkas said in another answer, when concatenating your strings you were not putting any space between day of month and year, but the format string you used for parsing required a space there.
  • If your month abbreviations are in English, for example, and your JVM’s default locale is not English, parsing will fail (except in the rare cases where the month abbreviation coincides). You should always give your formatter a locale to specify the language used in the string to be parsed (or produced).

As an aside, your variable name finalDatesInMMMDDYYYYFormat was misleading since a Date hasn’t got (cannot have) a format.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

If you want parse format "MMM dd yyyy", you should add an extra space to your test string like this:

final String testString = dateList.get(5) + ' ' + year;
rockfarkas
  • 132
  • 2
  • 8