【问题标题】:How to get long value from this date format "MMM dd, yyyy" on blackberry如何在黑莓上从此日期格式“MMM dd,yyyy”中获取长值
【发布时间】:2012-02-28 22:03:50
【问题描述】:

我收到了来自服务器的响应如下

<reminder><text>Hello Dude!</text><date>June 2, 2011</date></reminder>

我成功解析了信息。现在我需要添加关于黑莓提醒的信息。

我使用了以下代码:

try
{

    Event _event;

    String Calenderevent = "Hello Dude.";

    EventList eventList = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.WRITE_ONLY);

    _event = eventList.createEvent();

    long l= HttpDateParser.parse("June 2, 2011");

    _event.addString(Event.SUMMARY, PIMItem.ATTR_NONE,Calenderevent);
    _event.addDate(Event.START, PIMItem.ATTR_NONE, l);
    RepeatRule rule = new RepeatRule();
    rule.setInt(RepeatRule.FREQUENCY,RepeatRule.YEARLY);
    _event.setRepeat(rule);
    //If you need to repeat the event then use repeatrule.

    _event.commit();

    Dialog.alert("Calendar event success.");
} 
catch (PIMException e)
{
    Dialog.alert("Exception: "+e);
    e.printStackTrace(); 
}

当我在黑莓日历中看到时,信息显示在 2011 年 12 月 31 日

出现的问题在下面一行。

long l= HttpDateParser.parse("June 2, 2011");

它返回 -1 值。

如何在黑莓上从这种日期格式“MMM dd, yyyy”中获取长值。

请帮帮我。

【问题讨论】:

标签: blackberry java-me


【解决方案1】:

正如 Joel 所注意到的,HttpDateParser 不支持您的日期格式。一种可能的解决方案是将您的日期转换为HttpDateParser 支持的格式之一,然后使用parse() 方法对其进行解析。

此代码首先将您的日期转换为 Wdy, Mon DD YYYY HHMMSS 格式,然后对其进行解析。

    String date = "June 2, 2011";       
    String time = "120000"; // desired time HHMMSS

    long l = 0;

    try {
        StringBuffer sbDate = new StringBuffer();

        // append WEEKDAY. weekday is not relevant for the HttpParser.
        sbDate.append("Sun, ");

        // remove comma after month
        int commaIndex = date.indexOf(","); 
        sbDate.append(date.substring(0, commaIndex));
        sbDate.append(date.substring(commaIndex+1));

        sbDate.append(' ').append(time);

        l=HttpDateParser.parse(sbDate.toString());

    } catch (IndexOutOfBoundsException e) {
        // the date is in wrong format
    }

根据我所做的测试,HttpDateParser 没有考虑 Wdy。它在任何有效的工作日返回正确的结果。这对我来说很有意义,因为真正需要的是 DAY、MONTH 和 YEAR。

【讨论】:

  • 谢谢你的朋友。您的代码完美运行。非常感谢你。
猜你喜欢
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多