我有3条消息,这条消息是按顺序发送的,服务器给了我4个ISO8601时间:
2017-01-11T12:34:21.948631
2017-01-11T12:34:22.425915
2017-01-11T12:34:22.954749
2017-01-11T12:34:23.473965我的逻辑转换为当前日期
public class ISO8601{
static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSS");
String isodate;
long timestamp = 0;
public ISO8601(String isodate){
dateformat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.isodate = isodate;
try {
Date date = dateformat.parse(this.isodate);
timestamp = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
public long getTime(){
return timestamp;
}
}用于转换为日期的类
public class HelperMethods {
public static CharSequence getFormatTime(long time) {
DateFormat sdf = new SimpleDateFormat("hh:mm:ss yyyy-MM-dd");
Date netDat = new Date(time);
return sdf.format(netDat);
}
}现在我尝试使用这个类和方法将ISO8601转换为正常时间
Log.e("1", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:21.948631").getTime())));
Log.e("2", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:22.425915").getTime())));
Log.e("3", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:22.954749").getTime())));
Log.e("4", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:23.473965").getTime())));然后输出:
01:50:09 2017-01-11
01:41:27 2017-01-11
01:50:16 2017-01-11
01:42:16 2017-01-11如果我做* 1000L时间戳,如下所示:
public static CharSequence getFormatTime(long time) {
DateFormat sdf = new SimpleDateFormat("hh:mm:ss yyyy-MM-dd");
Date netDat = new Date(time * 1000L);
return sdf.format(time);
}它输出:
09:00:31 48999-02-14
08:05:15 48999-02-08
10:59:09 48999-02-14
09:42:45 48999-02-09我不明白为什么我不能直接转换日期
https://stackoverflow.com/questions/41591238
复制相似问题