我需要从zoom提供的uct日期和时区在java中创建一个date对象服务器的时区不能是一个因素

k3bvogb1  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(304)

也许这是一个边缘要求,因为找到一个有效的解决方案真的很棘手。。
我开了个会议
“时区”:“欧洲/柏林”,“创建时间”:“2020-11-20t19:35:22z”,
我想要一个java日期,当检查或输出(simpledateformat)时,它看起来像是在+时区的偏移处创建的。
考虑到我和柏林的时区不同,我试过的大多数路线都是根据系统日期进行调整,我绕不过去。
在经历了很多痛苦之后,我最终使用了这个方法(为了这篇文章的缘故,这个方法变得不那么通用)。我希望这能帮助一些人,如果有一个更简单的解决方案,我很想知道:)

rdlzhqv9

rdlzhqv91#

public static Date adjustDateTimeZone(String created_at, String timezone) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Date date = dateFormat.parse(created_at); //parse the date provided by Zoom
    Instant nowUtc = Instant.parse(Constants.dateFormat.format(date)); //Don't create using date.getMillis(). Because there are some weird adjustments that happen.
    ZoneId timezoneId = ZoneId.of(timezone);
    ZonedDateTime correctedDate = ZonedDateTime.ofInstant(nowUtc, timezoneId);
    //An example online showed using ZonedDateTime, and DateFormatter, but it did weird ass adjustments as well, which did not correspond to the 'toString()' output, 
    // which was correct.
    //Therefor I grabbed the twoString() output and created a date from that.
    return new SimpleDateFormat("yyyy-MM-ddHH:mm:ss").parse(correctedDate.toString().substring(0, 19).replaceAll("T", ""));
}
j0pj023g

j0pj023g2#

api的日期时间 java.util 以及它们的格式化api, SimpleDateFormat 过时且容易出错。我建议您完全停止使用它们,转而使用现代的日期时间api。

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.parse("2020-11-20T19:35:22Z");
        System.out.println(zdt);

        ZonedDateTime zdtAtBerlin = zdt.withZoneSameInstant(ZoneId.of("Europe/Berlin"));
        System.out.println(zdtAtBerlin);

        // Custom format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss'created_at'XXX");
        System.out.println(formatter.format(zdtAtBerlin));
    }
}

输出:

2020-11-20T19:35:22Z
2020-11-20T20:35:22+01:00[Europe/Berlin]
2020-11-20T20:35:22created_at+01:00

在trail:date-time了解有关现代日期时间api的更多信息。如果您正在为一个android项目工作,并且您的android api级别仍然不符合java-8,请检查通过desugaring提供的java8+api以及如何在android项目中使用threetenabp。
使用传统api:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        String dateTimeStr = "2020-11-20T19:35:22Z";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        Date date = sdf.parse(dateTimeStr);
        System.out.println(sdf.format(date));
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        System.out.println(sdf.format(date));

        // Some other format
        DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'created_at'XXX");
        sdf2.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        System.out.println(sdf2.format(date));
    }
}

输出:

2020-11-20T19:35:22Z
2020-11-20T20:35:22+01
2020-11-20T20:35:22created_at+01:00

注意不要硬编码 'Z' 在格式中。这个 'Z' 代表 Zulu 表示日期和时间 UTC .

相关问题