[英]Daylight savings time being added when string is converted to Timestamp in Java
我的應用程序接收字符串格式的時間戳,我們在SimpleDateFormat類中使用parse()
方法將該String轉換為Timestamp
對象。 我的應用程序在America / New_York時區運行,因此我們在3月9日2.00到2.59 am之間的時間戳面臨夏時制問題。 我們通過修改默認時區屬性以確保將所有夏時制字段都重置為零來解決此問題。 但是,當夏令時更改后(3月22日),我們使用java.util.Date類創建了當前時間戳記對象時,它顯示的時間比實際當前時間戳記少1小時。 這是因為更改了默認時區的屬性。
public class DateTest {
static TimeZone defaultTimeZone = TimeZone.getDefault();
Timestamp timestamp1;
public static void main(String[] args) throws Exception {
DateTest dateTest = new DateTest();
System.out.println("Current date before changing Timezone:" + new java.util.Date());
//Adds one hour extra to the actual timestamp
System.out.println("Converted Timestamp with Daylight:" + convertStringToTimestamp("2014030902101900"));
System.out.println("=====================================================================");
//Displays as the actual timestamp
dateTest.setTimestamp1(convertStringToTimestampWithoutDaylight("2014030902101900"));
System.out.println("Converted Timestamp with Daylight savings:" + dateTest.getTimestamp1());
//1 hour is reduced compared to current timestamp as Daylight savings time is removed
System.out.println("Current date after changing Timezone:" + new java.util.Date());
System.out.println("=====================================================================");
//Reset back to the original timezone
TimeZone.setDefault(defaultTimeZone);
Displays current timestamp
System.out.println("Current date after Timezone reset:" + new java.util.Date());
//Adds one hour again
System.out.println("Converted Timestamp after Timezone reset:" + dateTest.getTimestamp1());
System.out.println("=====================================================================");
}
}
我們在項目中同時使用當前時間戳對象和字符串到時間戳的轉換。 我也嘗試使用Joda-Time,但是最后,當Joda-Time轉換為Timestamp時,我們需要Timestamp對象,並添加了夏時制。
DateTime dateTime = DateTime.now();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime.toDate());
System.out.println("Current Timestamp:" + new Timestamp(calendar.getTimeInMillis()));
LocalDateTime localdateTime = LocalDateTime.parse("2014030902101100", DateTimeFormat.forPattern("yyyyMMddHHmmssSS"));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(localdateTime.toDate());
System.out.println("Converted Timestamp:" + new Timestamp(calendar2.getTimeInMillis()));
輸出:當前時間戳:2014-03-22 09:15:09.478
轉換時間戳記:2014-03-09 03:10:11.0
在LinkedIn上的並行線程中,您表示不知道時間戳對應於哪個時區。 在大多數情況下,這是不好的。 但是,如果確實存在這種情況,則可以隨意將它們視為您選擇的任何時區中的時間戳。 如果您不知道時間戳所對應的時區是否具有夏時制,並且是否遵循夏時制,則最安全的選擇是考慮它們屬於沒有夏時制的時區。 我建議使用UTC。 因此,在解析或格式化時間戳之前,您需要將SimpleDateFormat的時區設置為UTC:
public static void main(String[] args) throws Exception {
Date date = parseTimestamp("2014030902101100");
System.out.println("Parsed date: " + formatDate(date));
date = parseTimestamp("2014032202101100");
System.out.println("Parsed date: " + formatDate(date));
}
private static Date parseTimestamp(String timestamp) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(timestamp);
return date;
}
private static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS z");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(date);
}
輸出為:
Parsed date: 2014-03-09 02:10:11.00 UTC
Parsed date: 2014-03-22 02:10:11.00 UTC
您可以嘗試使用SimpleDateFormat解析字符串,例如
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmssSSz");
Date date = sd.parse("2014030902101100GMT-05:00");
對於z(時區)參數,您可以將GMT-05:00(紐約與GMT / UTC的偏移量)添加到手頭的時間戳字符串中。 在此之后,您可以轉換為所需的內容。
simpledateforamt.parse將為您提供日期(java.util.date)。 從日期到時間戳記的順應性很直接。
long ms = date.getTime();
Timestamp t = new Timestamp(ms);
DateTime dateTime = DateTime.now();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime.toDate());
System.out.println("Current Timestamp:" + new Timestamp(calendar.getTimeInMillis()));
LocalDateTime localdateTime = LocalDateTime.parse("2014030902101100",
DateTimeFormat.forPattern("yyyyMMddHHmmssSS"));
System.out.println("Converted Timestamp:" + Timestamp.valueOf(localdateTime.toString("yyyy-mm-dd hh:mm:ss")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.