[英]java convert milliseconds date corresponding timezone date in java
1)我需要找出用戶的時區和時間。 為此,我用
Calendar currentdate1 = Calendar.getInstance();
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println("time zone"+tz);
System.out.println(tz.getDisplayName()); // (Now India Standard Time)
System.out.println(tz.getID()); // (Now . Asia/Kolkata)
通過使用此方法,我需要找出相應時區的該user1的當前時間。
2)一個用戶2在該日期上傳視頻。 我將其轉換為毫秒,並將其存儲在數據庫中。 User1希望將user2的上傳時間視為與他的時區相對應的user1日期和時間。我該如何做。
由於每次存儲的時間都以毫秒為單位,因此無需進行轉換。 您所追求的是以正確的格式顯示時間(以毫秒為單位)。 請考慮以下情形:
User2在與日期t = 12345678911ms對應的日期上傳視頻(這是一個絕對值,與1970年1月1日00:00的偏移量相對應,不需要更改)
User1希望根據自己的時區查看哪個日期和時間對應於值t。 因此,在設置正確的日歷/時區之后
Calendar user1C = Calendar.getInstance();//Executed on the system of user1 this command will get the correct locale and timezone:
您只需要基於user2的值t在user1的日歷上設置時間:
user1C.setTimeInMillis(t);//Sets the the time of the current calendar based on the value of user2
如果現在打印日歷時間,您將看到與user2對應的日期和時間
System.out.println(user1C.getTime());
重要的是要理解, java.util.Date
對象是絕對時間點。 無法從該對象獲取時區。 可以解釋java.util.Date
后面的long值來獲取人們通常使用的日期(日期字符串)。 這取決於格式化程序對象使用的時區。
您還可以從日期字符串中獲取java.util.Date
對象。 結果取決於日歷對象用於轉換/解析的時區。
該示例可能會有所幫助:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Test {
public static void main(final String[] args) {
Date now = new Date();
printDateForBerlinAndKolkata(now, "Current Time:");
Calendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
Calendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("Asia/Kolkata"));
cal1.set(2014, 11, 5, 15, 30);
printDateForBerlinAndKolkata(cal1.getTime(), "Gregorian Calendar 2014-12-05 15:30 Timezone for Berlin:");
cal2.set(2014, 11, 5, 15, 30, 0);
printDateForBerlinAndKolkata(cal2.getTime(), "Gregorian Calendar 2014-12-05 15:30 Timezone for Kolkata:");
}
private static void printDateForBerlinAndKolkata(Date now, String headline) {
System.out.println(headline);
System.out.println("---------------------------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ssZ");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
System.out.println("Europe/Berlin: " + sdf.format(now));
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println("Asia/Kolkata: " + sdf.format(now));
System.out.println("long value: " + now.getTime());
System.out.println("=============================================\n");
}
}
輸出:
Current Time:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 16:35:21+0100
Asia/Kolkata: 2014 Dez 05 21:05:21+0530
long value: 1417793721481
=============================================
Gregorian Calendar 2014-12-05 15:30 Timezone for Berlin:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 15:30:21+0100
Asia/Kolkata: 2014 Dez 05 20:00:21+0530
long value: 1417789821506
=============================================
Gregorian Calendar 2014-12-05 15:30 Timezone for Kolkata:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 11:00:00+0100
Asia/Kolkata: 2014 Dez 05 15:30:00+0530
long value: 1417773600506
=============================================
也許以下鏈接也很有趣: Unix Time上的Wikipedia文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.