简体   繁体   中英

Convert timestamp to simple date in Java and add to ParseObject

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. ie 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]

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);
}
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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM