Date Format in android

牧云@^-^@ 提交于 2020-01-25 10:35:06

问题


I have date string of the format 8/1/2011 04:06 am. I want it to convert it in to the date string of the format 2011-08-1T16:06:00-04:00 using DateFormat. I am using the code

 private String formatDate(String date) {

    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); 
    Date dateObj = null;

        try {
            dateObj =  curFormater.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String format = "yyyy-MM-dd'T'HH:mmZ";

        String date = (String) DateFormat.format(format, dateObj);
        return date;
    }

But it returns the String 2011-01-08THH:37Z.

Can any one please help me with this?


回答1:


Try to replace this:

String date = (String) DateFormat.format(format, dateObj);

with this:

curFormater = new SimpleDateFormat(format);
date = curFormater.format(dateObj);

Also, take into account that your example is a bit misleading:

04:06 am should not be 16:06:00-04:00 but 04:06 pm should be 16:06:00-04:00.



来源:https://stackoverflow.com/questions/6898934/date-format-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!