Java Android date,回到夏天会不会出错?

Java Android date,回到夏天会不会出错?,java,android,datetime,Java,Android,Datetime,此日期是从我的应用程序发送的,如下所示: 2014-10-29 09:00:17 Wed Oct 29 2014 09:00:17 GMT+0100 (CET) public static long getUTCToMillis(String time) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'

此日期是从我的应用程序发送的,如下所示:

2014-10-29 09:00:17
Wed Oct 29 2014 09:00:17 GMT+0100 (CET)
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    sdf.setTimeZone(TimeZone.getTimeZone("CET")); 
    try {
        cal.setTime(sdf.parse(time));
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());

    TimeZone timezone = TimeZone.getTimeZone("CET");

    try {
        Date date = sdf.parse(time);
        long offSet = timezone.getOffset(date.getTime());
        cal.setTime(date);
        cal.add(Calendar.MILLISECOND, offset);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
数据库按如下方式存储它:

2014-10-29 09:00:17
Wed Oct 29 2014 09:00:17 GMT+0100 (CET)
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    sdf.setTimeZone(TimeZone.getTimeZone("CET")); 
    try {
        cal.setTime(sdf.parse(time));
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());

    TimeZone timezone = TimeZone.getTimeZone("CET");

    try {
        Date date = sdf.parse(time);
        long offSet = timezone.getOffset(date.getTime());
        cal.setTime(date);
        cal.add(Calendar.MILLISECOND, offset);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
我从我的服务器返回的日期如下所示:

2014-10-29T08:00:17.000Z (one our less)
然后我将其转换为如下日历日期:

2014-10-29 09:00:17
Wed Oct 29 2014 09:00:17 GMT+0100 (CET)
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    sdf.setTimeZone(TimeZone.getTimeZone("CET")); 
    try {
        cal.setTime(sdf.parse(time));
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());

    TimeZone timezone = TimeZone.getTimeZone("CET");

    try {
        Date date = sdf.parse(time);
        long offSet = timezone.getOffset(date.getTime());
        cal.setTime(date);
        cal.add(Calendar.MILLISECOND, offset);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
这总是将小时设置为08:00,但我希望它被转换回09:00,就像它被保存一样。如果我将时区设置为UTC,我会得到09:00,但当夏季时间再次开始时,这将如何工作?我怎样才能让它显示正确的时间呢?

看一看

该方法返回UTC和目标时区之间的时差

所以你可以这样做:

2014-10-29 09:00:17
Wed Oct 29 2014 09:00:17 GMT+0100 (CET)
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    sdf.setTimeZone(TimeZone.getTimeZone("CET")); 
    try {
        cal.setTime(sdf.parse(time));
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
public static long getUTCToMillis(String time) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());

    TimeZone timezone = TimeZone.getTimeZone("CET");

    try {
        Date date = sdf.parse(time);
        long offSet = timezone.getOffset(date.getTime());
        cal.setTime(date);
        cal.add(Calendar.MILLISECOND, offset);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}
作为补充说明,我建议您将时间值存储为UTC,而不管您的用户在哪里。 例如,如果应用程序同时在两个不同的时区中使用,则服务器应存储相同的值。时间转换将并且应该根据用户所在的时区在用户设备上本地完成