Android 时间戳转换

来源:互联网 发布:腾讯云已备案域名出售 编辑:程序博客网 时间:2024/04/29 01:29

服务器返回格式:2017-11-01T03:25:32.320Z

服务器的数据库使用MongoDB存储,时间格式如下:

ISODate("2017-11-01T03:25:32.320Z")

这个时间是格林尼治时间
转换代码

String Date(String GTMDate){        int tIndex = GTMDate.indexOf("T");        String dateTemp = GTMDate.substring(0, tIndex);        String timeTemp = GTMDate.substring(tIndex + 1, GTMDate.length() - 6);        String convertString = dateTemp + " " + timeTemp;        SimpleDateFormat format;        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);        Date result_date;        long result_time = 0;        if (null == GTMDate) {            return GTMDate;        } else {            try {                format.setTimeZone(TimeZone.getTimeZone("GMT00:00"));                result_date = format.parse(convertString);                result_time = result_date.getTime();                format.setTimeZone(TimeZone.getDefault());                return format.format(result_time);            } catch (Exception e) {                e.printStackTrace();            }        }        return GTMDate;    }
原创粉丝点击