在Java中将时间戳转换为简单日期并添加到ParseObject

时间:2022-03-06 02:29:26

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.

不重复:我打算用这个问题来解决在尝试将格式化日期添加回ParseObject以进行渲染时抛出的java.lang.IllegalArgumentException。

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.

我有一个日期列表,当我将它们渲染到我的页面时,我希望以更易读的格式显示这些日期。即我想要3月29日星期三13:32:35 CEST 2017成为3月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?

我认为SimpleDateFormat就够了,但我不能放弃额外的时间戳信息并将对象添加回我的收藏中。我该怎么办?

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]

2 个解决方案

#1


1  

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

我们总是可以将日期转换为所需格式的字符串并添加到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);
}

#2


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

#1


1  

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

我们总是可以将日期转换为所需格式的字符串并添加到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);
}

#2


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