Znacznik czasu UTC + czas Jody
Próbuję uzyskać UTC TimeStamp w prostym programie Java za pomocą Joda:
public Timestamp getCurrentUTC(LocalDateTime date, DateTimeZone srcTZ, DateTimeZone dstTZ, Locale l) {
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
System.out.println("UTC Time:" + dstDateTime.getMillis());
System.out.println("UTC Time:" + new Timestamp(dstDateTime.getMillis()));
return new Timestamp(dstDateTime.getMillis());
}
Wynik programu jest następujący:
UTC Time:1378265162047
UTC Time:2013-09-03 23:26:02.047
Wartość milisekundowa to poprawny czas UTC (tj. Potwierdzony za pomocąGMT-4
strefa czasowa) Druga wartość toEST
strefa czasowa.
Potrzebuję wartości UTC bez zmianjava.sql.Timestamp
(tj. niezależne od TZ), do zapisu bazy danych. czy to możliwe?
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
System.out.println("UTC Time:" + dstDateTime.getMillis());
wiem tosrcDateTime
jest datą lokalną (GMT-4) idstDateTime
to UTC (GMT-0). Wartości wyjściowe dat są następujące:
Source Date:2013-09-04T09:10:43.683-04:00
Destination Date: 2013-09-04T13:10:43.683Z
Próbowałem wszystkich kombinacji, aby spróbować uzyskać wartość UTCdstDateTime
jako java.sql.TimeStamp:
System.out.println("UTC Time:" + dstDateTime.getMillis());
System.out.println("UTC Time:" + new Timestamp(srcDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
System.out.println("UTC Time:" + new Timestamp(dstDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
Wydruk do testowania:
UTC Time:1378298760226 - Correct UTC
UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
Pierwsza linia wydruku to poprawny znacznik czasu UTC. Potrzebuję tylko tej samej wartości co java.sql.TimeStamp. Wszystko, co próbowałem, zawsze zwracało lokalną datę urządzenia.
Edytuj 2Próbowałem:
System.out.println("UTC Timestamp:" + date.toDateTime(srcTZ).getMillis());
System.out.println("UTC Timestamp:" + new Timestamp(date.toDateTime(srcTZ).getMillis()));
Dane wyjściowe są następujące:
UTC Time:1378342856315 - Correct UTC Time
UTC Timestap:2013-09-04 21:00:56.315 - Local Time other than the expected UTC Time
Ilekroć próbuję przekonwertować na TimeStamp, tracę prawidłową wartość UTC, po której jestem.
Pod względem parametrów metody:
srcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Montreal")
dstTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Etc/UTC"))
Local l = new Locale("en", "CA")
Każda pomoc jest bardzo mile widziana.
Nacięcie.
Edytuj 3Cześć Matt,
Bardzo Ci dziękuję za odpowiedź. Otrzymujemy takie same wyniki jak Ty. Nie wiedziałem nic o drukowaniu itp. Dokładniej:
System.out.println("UTC Timestamp:" + srcDateTime.toDateTime(dstTZ).getMillis());
System.out.println("UTC Timestamp:" + srcDateTime.toDateTime(dstTZ));
System.out.println("UTC Timestamp:" + new Timestamp(srcDateTime.toDateTime(dstTZ).getMillis()));
Wydajność:
UTC Timestamp:1378389098468 - Correct UTC Timestap (Thu, 05 Sep 2013 13:51:38 GMT)
UTC Timestamp:2013-09-05T13:51:38.468Z - Correct UTC Time
UTC Timestamp:2013-09-05 09:51:38.468 - Local time is printed, UTC is expected
Problem został mi zwrócony, gdy zdaliśmy sobie sprawę, że DB przechowuje czas lokalny zamiast UTC:
+---------------------+
| effectivedate |
+---------------------+
| 2013-09-05 09:34:11 |
+---------------------+
Strefa czasowa Mysql jest ustawiona na „-00: 00”
mysql> SELECT CURRENT_TIMESTAMP;
+---------------------+
| CURRENT_TIMESTAMP |
+---------------------+
| 2013-09-05 13:48:09 |
+---------------------+
Debugowanie aplikacji przy użyciu debugera eclipse zdaliśmy sobie sprawę, że lokalny czas (2013-09-05 09: 51: 38.468) był przekazywany do DB (nie można publikować obrazów, za mało punktów ...). Typ danych to prosty TimeStamp, bez manipulacji ciągiem. Może używa debugera eclipseString.println()
funkcja również, nie jestem pewien ..
Naprawdę doceniam pomoc w debugowaniu naszej aplikacji. Nie chciałem zajmować tak dużo czasu (bez gry słów) i wysiłku ...
Z poważaniem,
Nacięcie.