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小时差异