我面臨着一個問題。 假設用戶在2014-12-24 01:00從中國登錄我的網站,所以我保存了他的登錄時間。現在,如果他從法國再次登錄,所以我需要根據法國時區顯示他上次登錄時間,2014-12-23 17:00按照GMT。我有從他在登錄的用戶區域設置信息 這是我的代碼:以GMT從現場獲取時間
String lastLoggedIndatetime = "2014-11-23 04:01" ; //time of last logged in from China;
Locale locale = Locale.FRANCE; //suppose this i am getting as args from Http request
SimpleDateFormat loggedInDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm");
Calendar c = Calendar.getInstance(locale);
TimeZone loggedInTimeZone = c.getTimeZone();
System.out.println("Time Zone is "+ c.getTimeZone().getDisplayName());
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm");
TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTimeZone);
try {
String lastLoggedIndatetimeAsPerFrance = loggedInDateFormat.format(new SimpleDateFormat("yyyy-mm-dd hh:mm").parse(lastLoggedIndatetime.trim()));
String gmtDateTime= gmtFormat.format(new SimpleDateFormat("yyyy-mm-dd hh:mm").parse(lastLoggedIndatetimeAsPerFrance.trim()));
System.out.println("last logged in Date as per France is "+ lastLoggedIndatetimeAsPerFrance + " TimeZone is "+loggedInTimeZone);
System.out.println("GMT Date is "+ gmtDateTime + " TimeZone is "+ gmtTimeZone);
//formattedDate.append(gmtDateTime)
//.append(" - "); //" - " is delimiter for date
} catch (ParseException ex) {
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
和輸出是:
Time Zone is India Standard Time
last logged in Date as per France is 2014-01-23 04:01 TimeZone is sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
GMT Date is 2014-31-22 10:31 TimeZone is sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
它沒有顯示按法國,但按印度時間(目前我在印度)。格林尼治標準時間按照印度而不是法國。
你沒有將'locale'信息傳遞給'SimpleDateFormat',你可以嘗試通過'locale' – sol4me
仍然不能工作,得到相同的o/p ::時區是印度標準時間 – kcs