0

I'm attempting to parse a new Date object, but I keep hitting the following error.

W/System.err: java.text.ParseException: Unparseable date: "Thu May 16 09:28:39 GMT+01:00 2019"

I've attempted various different patterns for dateFormat, but nothing seems to work.

This is where the error is.

c.setTime(dateFormat.parse(oldDate));

Code

  public static String addDay(int numberOfDays) {

    String oldDate = String.valueOf(new Date());

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
    Calendar c = Calendar.getInstance();

    try {
     c.setTime(dateFormat.parse(oldDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    c.add(Calendar.DAY_OF_YEAR,numberOfDays);
    dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss",  Locale.ENGLISH);
    Date newDate=new Date(c.getTimeInMillis());
    String resultDate=dateFormat.format(newDate);
    return resultDate;
}
flatwhite
  • 27
  • 4
  • 5
    Well, the date doesn't match the format at all, e.g. `dd-MM-yyyy` means something like `16-05-2019` which is _not_ `Thu May 16 ... 2019`. – Thomas May 16 '19 at 08:40
  • You shouldn't use `Calendar`, `Date` and `SimpleDateFormat` classes. They're outdated, and [flawed](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api). Better use the new Java Date and Time API, from the `java.time` package, or else the ThreeTen ABP. – MC Emperor May 16 '19 at 09:08
  • @Michael I'm sure they're rotating in their graves ;) - Seriously: `Date.toString()` uses the format `EEE MMM dd HH:mm:ss zzz yyyy` (see the source code, the javadoc describes it in a little different terms that need "transformation"). – Thomas May 16 '19 at 09:08

3 Answers3

0

Try This function

In your question you are converting Date to string

then after you are once again Parsing String to Date

that is very Long way. you can directly set as c.setTime(oldDate);

  public static String addDay(int numberOfDays) {
    Date oldDate = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
    Calendar c = Calendar.getInstance();
    c.setTime(oldDate);
    c.add(Calendar.DAY_OF_YEAR,numberOfDays);
    dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss",  Locale.ENGLISH);
    Date newDate=new Date(c.getTimeInMillis());
    String resultDate=dateFormat.format(newDate);
    return resultDate;
  }
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
  • ... and the Date object is not even needed – Scary Wombat May 16 '19 at 08:46
  • This would be a better answer if you explained what you changed. At first glance it is quite similar. – Michael May 16 '19 at 08:47
  • @ScaryWombat because converting old date to string, then after convert into date . and parse . its very long way, try it old date in Date object directly directly – Nikunj Paradva May 16 '19 at 08:47
  • @Michael check it out edited answer , and i dont know why i downvotted. – Nikunj Paradva May 16 '19 at 08:52
  • If you read the javadocs you would read *Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:*, hence Date is not needed at all – Scary Wombat May 17 '19 at 01:03
0

The pattern should be like :

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                                            Locale.ENGLISH);

Then to print you need a second SimpleDateFormat:

Date parsedDate = sdf.parse(date);

SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");

System.out.println(print.format(parsedDate));
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
RaVin
  • 63
  • 1
  • 8
0

Your pattern is wrong. You should use EEE MMM dd HH:mm:ss z yyyy

Ahmadul Hoq
  • 705
  • 5
  • 14