Jak uzyskać przesunięcie strefy czasowej w GMT(jak GMT+7: 00) z urządzenia z systemem android?
Otrzymuję timezone
Urządzenia z Androidem za pomocą tego kodu
TimeZone tz = TimeZone.getDefault();
String current_Time_Zone = (TimeZone.getTimeZone(tz.getID()).getDisplayName(
false, TimeZone.SHORT))
Ale zawsze zwraca mi timezone
Jak "IST
" ale chcę mieć strefę czasową w GMT
Jak to GMT+7:00.
17 answers
To może dać ci pomysł, jak wdrożyć go do swoich upodobań:
Calendar mCalendar = new GregorianCalendar();
TimeZone mTimeZone = mCalendar.getTimeZone();
int mGMTOffset = mTimeZone.getRawOffset();
System.out.printf("GMT offset is %s hours", TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS));
(TimeUnit to " java.util./ align = "left" / TimeUnit")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-24 13:14:53
Ten kod zwraca mi przesunięcie GMT.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.getDefault());
Date currentLocalTime = calendar.getTime();
DateFormat date = new SimpleDateFormat("Z");
String localTime = date.format(currentLocalTime);
Zwraca przesunięcie strefy czasowej w następujący sposób: +0530
Jeśli użyjemy SimpleDateFormat poniżej
DateFormat date = new SimpleDateFormat("z",Locale.getDefault());
String localTime = date.format(currentLocalTime);
Zwraca przesunięcie strefy czasowej w następujący sposób: GMT+05:30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-23 04:11:21
Oto rozwiązanie, aby uzyskać przesunięcie strefy czasowej w GMT + 05: 30 ten format
public String getCurrentTimezoneOffset() {
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
offset = "GMT"+(offsetInMillis >= 0 ? "+" : "-") + offset;
return offset;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-24 11:52:18
public static String timeZone()
{
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
String timeZone = new SimpleDateFormat("Z").format(calendar.getTime());
return timeZone.substring(0, 3) + ":"+ timeZone.substring(3, 5);
}
Zwraca jak +03:30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-16 19:16:37
Rozwiązaniem jednowierszowym jest użycie symbolu Z
w postaci:
new SimpleDateFormat(pattern, Locale.getDefault()).format(System.currentTimeMillis());
Gdzie pattern
może być:
- Z / ZZ/ ZZZ: -0800
- ZZZZ: GMT-08: 00
- ZZZZZ: -08: 00
Pełne odniesienie tutaj:
Http://developer.android.com/reference/java/text/SimpleDateFormat.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-21 17:57:52
public static String getCurrentTimezoneOffset() {
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
return offset;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-21 22:29:30
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-31 12:09:36
Yet another solution to get timezone offset:
TimeZone tz = TimeZone.getDefault();
String current_Time_Zone = getGmtOffsetString(tz.getRawOffset());
public static String getGmtOffsetString(int offsetMillis) {
int offsetMinutes = offsetMillis / 60000;
char sign = '+';
if (offsetMinutes < 0) {
sign = '-';
offsetMinutes = -offsetMinutes;
}
return String.format("GMT%c%02d:%02d", sign, offsetMinutes/60, offsetMinutes % 60);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-11 13:26:05
Jeśli ktoś szuka sposobu reprezentacji GMT jako liczby float reprezentującej przesunięcie godziny
(na przykład "GMT-0530" do -5.5), możesz użyć tego:
Calendar calendar = new GregorianCalendar();
TimeZone timeZone = calendar.getTimeZone();
int offset = timeZone.getRawOffset();
long hours = TimeUnit.MILLISECONDS.toHours(offset);
float minutes = (float)TimeUnit.MILLISECONDS.toMinutes(offset - TimeUnit.HOURS.toMillis(hours)) / MINUTES_IN_HOUR;
float gmt = hours + minutes;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-13 10:28:20
Możesz zrobić tak:
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset();
String timeZone = String.format("%s%02d%02d", offset >= 0 ? "+" : "-", offset / 3600000, (offset / 60000) % 60);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-21 02:28:00
TimeZone timeZone = TimeZone.getDefault();
String timeZoneInGMTFormat = timeZone.getDisplayName(false,TimeZone.SHORT);
Wyjście : GMT + 5: 30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 15:17:19
TimeZone tz = TimeZone.getDefault();
String gmt1=TimeZone.getTimeZone(tz.getID())
.getDisplayName(false,TimeZone.SHORT);
String gmt2=TimeZone.getTimeZone(tz.getID())
.getDisplayName(false,TimeZone.LONG); Log.d("Tag","TimeZone : "+gmt1+"\t"+gmt2);
Zobacz czy to pomoże:)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-20 07:59:54
Też na to patrzyłem i próbowałem dowiedzieć się, jak zastosować strefę czasową i czas DST. Oto Mój kod.
public long applyGMTOffsetDST(long time) {
// Works out adjustments for timezone and daylight saving time
Calendar mCalendar = new GregorianCalendar();
TimeZone mTimeZone = mCalendar.getTimeZone();
boolean dstBool = mTimeZone.getDefault().inDaylightTime( new Date() );
// add an hour if DST active
if (dstBool == true) {
time = time + secondsPerHour;
}
// add offest hours
int mGMTOffset = mTimeZone.getRawOffset();
if (mGMTOffset > 0) {
long offsetSeconds = secondsPerHour * mGMTOffset;
time = time + offsetSeconds;
}
return time;
}
To wydaje się działać, ale czy istnieje lepszy sposób, aby uzyskać rzeczywisty czas wyłączenia urządzenia, który reprezentuje czas, który jest sensowny i dokładny dla użytkownika?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 19:28:35
Natknąłem się na proste rozwiązanie tego problemu w Java8 (Nie-Android) przy użyciu klasy ZoneDateTime
. Mogą być inne klasy implementujące interfejs TemporalAccessor
, które działają, ale ich nie znalazłem. To nie zadziała ze standardowym Date
, DateTime
, LocalDateTime
, i Calender
klasy z tego co wiem.
ZoneOffset myOffset = ZonedDateTime.now().getOffset();
ZoneOffset myOffset2 = ZoneOffset.from(ZonedDateTime.now());
log.info("ZoneOffset is " + myOffset.getId()); // should print "+HH:MM"
log.info("ZoneOffset2 is " + myOffset2.getId()); // should print "+HH:MM"
Zaletą tego rozwiązania jest to, że unika się wielu obliczeń modulo, generowania łańcuchów i parsowania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 23:52:07
Dodanie przesunięcia dst rozwiąże ten problem:
int offsetInMillis = TimeZone.getDefault().getRawOffset()+TimeZone.getDefault().getDSTSavings();
String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
return offset;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-19 07:20:55
W ten sposób Google zaleca uzyskanie TimezoneOffset.
Calendar calendar = Calendar.getInstance(Locale.getDefault());
int offset = -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)
Https://developer.android.com/reference/java/util/Date#getTimezoneOffset()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-08 06:47:33
TimeZone timeZone = TimeZone.getTimeZone ("GMT+7:00");
Zadeklaruj to jasno.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-25 13:35:47