Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以这种格式转换SQL时间戳;1394039043000“;串_Android_Timestamp_Simpledateformat - Fatal编程技术网

Android 如何以这种格式转换SQL时间戳;1394039043000“;串

Android 如何以这种格式转换SQL时间戳;1394039043000“;串,android,timestamp,simpledateformat,Android,Timestamp,Simpledateformat,我使用下面的方法,但是转换的时间戳有一个小时的差异 public static String getServerFormattedDate(String dateStr) { Timestamp ts = new Timestamp(Long.valueOf(dateStr)); // input date in // Timestamp

我使用下面的方法,但是转换的时间戳有一个小时的差异

public static String getServerFormattedDate(String dateStr) {


    Timestamp ts = new Timestamp(Long.valueOf(dateStr)); // input date in
                                                                    // Timestamp
                                                                    // format

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            Calendar cal = Calendar.getInstance();
            cal.setTime(ts);
            cal.add(Calendar.HOUR, +7); // Time different between UTC and PDT is +7
                                        // hours
            String convertedCal = dateFormat.format(cal.getTime()); // This String
                                                                    // is converted
                                                                    // datetime
            /* Now convert String formatted DateTime to Timestamp */

            return convertedCal;
        }

不要自己在时间戳上计算时区。相反,将其设置为UTC,并在
日期格式上设置时区。例如:

SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = new Date(Long.valueOf(dateStr));
String convertedCal = dateFormat.format(date);
默认情况下,
SimpleDataFormat
使用适合当前默认语言环境的时区设置,这可以解释您看到的1小时差异