服务器时区java转换

发布于 2024-12-05 10:10:44 字数 212 浏览 1 评论 0原文

好的,所以我几乎尝试了一切。我敢打赌这确实很简单,但我似乎无法掌握它。

服务器向我发送时间,即纪元。然而,当我将其放入日期对象时,它似乎会自动选择时区,并将服务器时间添加+3。因此,如果 GMT 时间为 00.00,则它会显示为 03.00。

我还需要添加自己的时区。假设纪元时间再次为 00.00,添加时区后它应该显示为 10.00。

任何帮助将不胜感激。谢谢

Ok, so I've pretty much tried everything. I bet it's something really simple but I can't seem to get a hold of it.

The server sends me the time, which is epoch. However when I put this into a date object it seems to automatically pick up the time zone and it adds +3 to the server time. So if the gmt time is 00.00, it says its 03.00.

I also need to add a timezone of my own. Let's say the epoch time is 00.00 again, it should read 10.00 after I add the timezone.

any help would be much appreciated. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不放开 2024-12-12 10:10:44

“似乎添加了” - 我怀疑您正在使用 Date.toString() 它确实使用了本地时区。不过,Date 对象本身实际上是 UTC。使用 DateFormat 执行到字符串的转换,并且您可以指定要使用的时区。您可能还需要使用日历 - 这取决于您想要做什么。

(或者,首先使用 Joda Time,这是一个更好的 API。它可能有点庞大不过,对于你的 Android 项目,如果某个地方有一个“Joda Time lite”项目正好适合这类事情,我不会感到惊讶......)

编辑:快速示例,尽管并不完全清楚你需要什么......

long millis = getMillisFromServer();
Date date = new Date(millis);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(customTimeZone);
String formatted = format.format(date);

"It seems to add" - I suspect you're using Date.toString() which does indeed use the local time zone. The Date object itself is effectively in UTC though. Use DateFormat to perform the conversion to a string instead, and you can specify which time zone to use. You may also need to use Calendar - it depends what you're trying to do.

(Alternatively, use Joda Time in the first place, which is a better API. It may be a little bulky for your Android project though. I wouldn't be surprised if there were a "Joda Time lite" project around somewhere for precisely this sort of thing...)

EDIT: Quick sample, although it's not entirely clear what you need...

long millis = getMillisFromServer();
Date date = new Date(millis);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(customTimeZone);
String formatted = format.format(date);
巴黎盛开的樱花 2024-12-12 10:10:44

java.time

java.util Date-Time API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*

另外,下面引用的是Joda-Time主页的通知< /a>:

请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它将取代该项目。

使用现代日期时间 API java.time 的解决方案:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        long millis = 1316391494L;

        Instant instant = Instant.ofEpochMilli(millis);
        System.out.println(instant);

        // The same instant at a specific timezone
        ZonedDateTime zdt = instant.atZone(ZoneId.of("Australia/Brisbane"));
        System.out.println(zdt);
    }
}

输出:

1970-01-16T05:39:51.494Z
1970-01-16T15:39:51.494+10:00[Australia/Brisbane]

在线演示

跟踪:日期时间

你的代码出了什么问题?

java.util.Date 对象仅表示时间轴上的一个时刻 - 自 UNIX 纪元(1970 年 1 月 1 日,00:00:00 GMT)以来的毫秒数的包装器。由于它不包含任何时区信息,因此其 toString 函数应用 JVM 时区以返回格式为 EEE MMM dd HH:mm 的 String :sszz yyyy,源自此毫秒值。要获取不同格式和时区的 java.util.Date 对象的 String 表示形式,您需要使用具有所需格式的 SimpleDateFormat和适用的时区,例如

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        long millis = 1316391494L;
        Date date = new Date(millis);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX[zzzz]", Locale.ENGLISH);

        sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String strDateUtc = sdf.format(date);
        System.out.println(strDateUtc);

        sdf.setTimeZone(TimeZone.getTimeZone("Australia/Brisbane"));
        String strDateBrisbane = sdf.format(date);
        System.out.println(strDateBrisbane);
    }
}

输出:

1970-01-16T05:39:51.494Z[Coordinated Universal Time]
1970-01-16T15:39:51.494+10:00[Australian Eastern Standard Time]

在线演示


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        long millis = 1316391494L;

        Instant instant = Instant.ofEpochMilli(millis);
        System.out.println(instant);

        // The same instant at a specific timezone
        ZonedDateTime zdt = instant.atZone(ZoneId.of("Australia/Brisbane"));
        System.out.println(zdt);
    }
}

Output:

1970-01-16T05:39:51.494Z
1970-01-16T15:39:51.494+10:00[Australia/Brisbane]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

What went wrong with your code?

A java.util.Date object simply represents an instant on the timeline — a wrapper around the number of milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 GMT). Since it does not hold any timezone information, its toString function applies the JVM's timezone to return a String in the format, EEE MMM dd HH:mm:ss zzz yyyy, derived from this milliseconds value. To get the String representation of the java.util.Date object in a different format and timezone, you need to use SimpleDateFormat with the desired format and the applicable timezone e.g.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        long millis = 1316391494L;
        Date date = new Date(millis);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX[zzzz]", Locale.ENGLISH);

        sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String strDateUtc = sdf.format(date);
        System.out.println(strDateUtc);

        sdf.setTimeZone(TimeZone.getTimeZone("Australia/Brisbane"));
        String strDateBrisbane = sdf.format(date);
        System.out.println(strDateBrisbane);
    }
}

Output:

1970-01-16T05:39:51.494Z[Coordinated Universal Time]
1970-01-16T15:39:51.494+10:00[Australian Eastern Standard Time]

ONLINE DEMO


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文