StackOverflow Android get Current UTC time と How can I get the current date and time in UTC or GMT in Java? でしばらくの間、質問を掘り下げてきました 。
携帯電話の現在時刻を GMT で取得する方法を 2 つ試しました。私はスペインにいますが、差は GMT+2 です。それでは例を見てみましょう: 1º 試み: フォーマットを作成し、それを System.currentTimeMillis();
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = dfgmt.format(new Date());
//Using System.currentTimeMillis() is the same as new Date()
Date dPhoneTime = dfgmt.parse(gmtTime);
Long phoneTimeUTC = dPhoneTime.getTime();
その時間を別の時間に差し引く必要があるため、Long にキャストします。
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date arrivalDate = df.parse(item.getArrivalDate());
//the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
//which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
diff = arrival.getTime() - phoneTimeUTC ;
私もこれを試しました:
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()
それでも、正しい違いがわかりません。しかし、私がこれを行うと:
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;
それは正常に動作します。
何か案は?
どうもありがとう、
デビッド。