7

私はJavaでソフトウェアを開発しています。

サーバーからGMTのタイムスタンプを取得します。このソフトウェアは、世界中のどこでも使用できます。次に、ソフトウェアが実行されているローカルタイムゾーンを取得し、このGMT時間をローカル時間に変換します。

これを行う方法を教えてください。

4

4 に答える 4

4

timestampあなたがまたはのいずれかであるDateと仮定しますNumber

final DateFormat formatter = DateFormat.getDateTimeInstance();
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(formatter.format(timestamp));

タイムスタンプがとして指定されている場合はString、最初にそれを解析する必要があります。には、カスタム形式の例がたくさんあります。SimpleDateFormat組み込み形式の簡単な例は次のとおりです。

final DateFormat formatter = DateFormat.getDateTimeInstance();
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
final Date timezone = formatter.parse("2012-04-14 14:23:34");
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(formatter.format(timezone));
于 2012-04-14T12:25:18.357 に答える
4

ローカルタイムゾーンを取得するには:

Calendar.getInstance()。getTimeZone()。getDisplayName()

変換の場合:

Javaでの日付TimeZone変換?

于 2012-04-14T11:51:23.687 に答える
0

Joda-Timeをご覧ください。それは人が必要とするすべての日付関連の機能を持っています

于 2012-04-14T11:41:35.970 に答える
0

yyyy/MM/dd HH:mm:ss.SSSこれが機能する形式のサーバー時間を想定すると、次のようになります。

String serverTime = "2017/12/06 21:04:07.406"; // GMT
ZonedDateTime gmtTime = LocalDateTime.parse(serverTime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS")).atZone(ZoneId.of("GMT"));
LocalDateTime localTime = gmtTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
于 2017-12-14T19:35:05.210 に答える