![](/img/trans.png)
[英]How to convert time in utc second and timezone offset in second to date using java-8?
[英]How to convert date for the first timezone to the second?
有以下信息:
我的應用從某台服務器獲取此信息,因此我需要將此日期/時間轉換為當前用戶的時區。 我知道我應該使用DateFormat和TimeZone API,但我不知道如何使用。 請給我一些建議或一些代碼。 謝謝。
下面的代碼將為您工作, 並為java的timezones-in引用此堆棧問題:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
cal.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date resultdate = new Date(cal.getTimeInMillis());
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
System.out.println("String date:"+sdf.format(resultdate));
try {
System.out.println("Date:"+sdf2.parse(sdf.format(resultdate)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
輸出值
String date:2013-08-31 06:25:54 PM
Date:Sat Aug 31 18:25:54 IST 2013
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.