1

Not Duplicate: I intended for this question to address the java.lang.IllegalArgumentException that is thrown when attempting to add a formatted date back to a ParseObject for rendering purposes.

I've got a list of dates which I want to display in a more readable format when I render them to my page. i.e. I want Wed Mar 29 13:32:35 CEST 2017 to become Wed Mar 29.

for (ParseObject requestObject: requestsArrayList) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = sdf.parse(sdf.format(requestObject.getDate(ParseConstantsUtil.REQUEST_DATE_REQUESTED)));
        log.info(String.valueOf(date));
    } catch (java.text.ParseException e1) {
        e1.printStackTrace();
    }
    requestObject.add(ParseConstantsUtil.REQUEST_DATE_REQUESTED, date);
}
requestObject.add(ParseConstantsUtil.REQUEST_DATE_REQUESTED, date);

I thought SimpleDateFormat would be enough but I can't ditch the additional timestamp info and add the object back to my collection. What should I do?

Exception:

java.lang.IllegalArgumentException: not implemented!
    at org.parse4j.operation.AddOperation.apply(AddOperation.java:26) ~[parse4j-1.5-SNAPSHOT.jar:na]
    at org.parse4j.ParseObject.performOperation(ParseObject.java:375) ~[parse4j-1.5-SNAPSHOT.jar:na]
    at org.parse4j.ParseObject.addAll(ParseObject.java:301) ~[parse4j-1.5-SNAPSHOT.jar:na]
    at org.parse4j.ParseObject.add(ParseObject.java:296) ~[parse4j-1.5-SNAPSHOT.jar:na]
    at com.nnit.automation.controller.IndexController.findRequestsByCurrentUser(IndexController.java:117) ~[classes/:na]
    at com.nnit.automation.controller.IndexController.findRequestsByCurrentUser(IndexController.java:61) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 2
    http://stackoverflow.com/questions/11097256/how-to-convert-mon-jun-18-000000-ist-2012-to-18-06-2012 – XtremeBaumer Apr 07 '17 at 10:03
  • What result are you getting with SimpleDateFormat? – Douglas Hosea Apr 07 '17 at 10:03
  • The log spits out the same date format. Maybe I'm confused about what I want. I just want to remove the timestamp and timezone from the date: ``2017-04-07 12:08:33.637 INFO 224456 --- [nio-8081-exec-6] com.test.date.WebApplication : Wed Mar 29 00:00:00 CEST 2017``. Seems like I already have SimpleDateFormat. – Martin Erlic Apr 07 '17 at 10:09
  • You seem to be logging the result of `Date.toString()`. A `Date` object *just* represents a point in time - it doesn't know about a time zone or format. If you want to log the `Date` in a particular format, you need to do the formatting *when you log*. – Jon Skeet Apr 07 '17 at 10:13
  • Possible duplicate of [How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012?](http://stackoverflow.com/questions/11097256/how-to-convert-mon-jun-18-000000-ist-2012-to-18-06-2012) – Software Engineer Apr 07 '17 at 10:13

2 Answers2

1

We can always convert the date to string in the needed format and add to requestObject

Sample Updated

for (ParseObject requestObject: requestsArrayList) {
SimpleDateFormat sdf2 = new SimpleDateFormat("E MMM dd");
String date = null;
try {
    date = sdf.format(requestObject.getDate(ParseConstantsUtil.REQUEST_DATE_REQUESTED));
    log.info(String.valueOf(date));
} catch (java.text.ParseException e1) {
    e1.printStackTrace();
}
requestObject.add(ParseConstantsUtil.REQUEST_DATE_REQUESTED, date);
}
Abdul Rahman
  • 446
  • 2
  • 8
  • I'm getting a ``java.text.ParseException: Unparseable date: "Fri Feb 24 18:27:07 CET 2017"`` with this one... – Martin Erlic Apr 07 '17 at 10:19
  • Here ``Wed Mar 29 00:00:00 CEST 2017`` – Martin Erlic Apr 07 '17 at 10:23
  • Okay so this definitely formats the date correctly, i.e. ``Fri Apr 14`` but it gives ``java.lang.IllegalArgumentException: not implemented!`` when I try to add it back to the object. Do you have any idea why that would happen? – Martin Erlic Apr 07 '17 at 10:30
  • It may mean that requestObject.add(, ) method is not implemented. Could you please give a little more context. Where this code is used, Are you trying to call an api, any specific frameworks used. – Abdul Rahman Apr 07 '17 at 10:38
  • I'm using the library Parse4J. Here is the ParseObject that I'm iterating over: https://github.com/thiagolocatelli/parse4j/blob/master/src/main/java/org/parse4j/ParseObject.java – Martin Erlic Apr 07 '17 at 10:40
  • I suppose Parse only handles one kind of Date format. Maybe I need to create a local POJO and add a new getter for the formatted date... – Martin Erlic Apr 07 '17 at 10:44
  • Good that you found the solution. Happy to help! – Abdul Rahman Apr 07 '17 at 10:45
1
try {
    SimpleDateFormat inputDate = new SimpleDateFormat("EEE MMM HH:mm:ss yyyy", Locale.US);
    SimpleDateFormat outputDate = new SimpleDateFormat("EEE MMM dd", Locale.US);

    String date = outputDate.format(inputDate.parse(input.replace("CEST ","")));


} catch (ParseException e) {
    e.printStackTrace();
}
KKSINGLA
  • 1,284
  • 2
  • 10
  • 22