字符串到日期格式在j2me中

时间:2012-08-23 09:16:04

标签: java-me lwuit

我有这样的字符串Wed, 22 Aug 2012 06:29:31 +0530,现在我想将此String转换为日期格式,我需要显示日期Wed, 22 Aug 2012这样并消除其他剩余字符串?

我需要在LWUIT表单屏幕上显示日期

5 个答案:

答案 0 :(得分:2)

String date = "Wed, 22 Aug 2012 06:29:31 +0530";
String shortDate = date.substring(0, 16);

答案 1 :(得分:2)

您需要使用java.util.Calendar中的方法手动编写代码,我们在Codename One中执行了一些格式化程序和本地化API,以便您可以查看我们的代码以供参考,或者只需转到Codename One

答案 2 :(得分:2)

对于此日期格式“发布日期”:“2012-04-19T16:45:33 + 01:00”我使用以下代码。您可以根据自己的需要进行自定义。

 try {
                String dateString = date_posted;
                String[] dateArray = Util.split(dateString, "T");
                String[] date = Util.split(dateArray[0], "-");
                String[] time = Util.split(dateArray[1], ":");
            Calendar calStart = Calendar.getInstance();
            calStart.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calStart.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
            calStart.set(Calendar.YEAR, Integer.parseInt(date[0]));
            calStart.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
            calStart.set(Calendar.MINUTE, Integer.parseInt(time[1]));
            calStart.set(Calendar.MILLISECOND, 0);

            Date postDate = calStart.getTime();

            post_time = postDate.getTime();
        } catch (Exception ex) {
            Logger.err("fromJSON", ex);
        }

答案 3 :(得分:2)

我使用这样的东西:

String getDateString(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);

    return new String(/*Any format you need*/);
}

答案 4 :(得分:1)

j2me中没有将字符串转换为日期的具体方法。所以,根本不可能