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

时间:2017-04-07 10:00:45

标签: java date parse-platform simpledateformat parse4j

不重复:我打算在此问题中解决在尝试将格式化日期添加回java.lang.IllegalArgumentException以进行渲染时引发的ParseObject

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

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

例外:

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 个答案:

答案 0 :(得分:1)

我们总是可以将日期转换为所需格式的字符串并添加到requestObject

示例已更新

<menu xmlns:android="http://schemas.android.com/apk/res/android"
         <group android:id="@+id/grp1" android:checkableBehavior="single" >
            <item
                android:id="@+id/navigation_item_1"
                android:checked="true"
                android:icon="@drawable/ic_home"
                android:title="@string/navigation_item_1" />
        </group>
 <group android:id="@+id/grp2" android:checkableBehavior="single" >
            <item
                android:id="@+id/navigation_item_1"
                android:checked="true"
                android:icon="@drawable/ic_home"
                android:title="@string/navigation_item_1" />
        </group>
    </menu>

答案 1 :(得分: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();
}