从date转换为localdatetime,格式为“yyyy-mm-dd hh:mm:ss”

z9smfwbn  于 2021-07-07  发布在  Java
关注(0)|答案(4)|浏览(793)

我现在有一个日期,例如“2015-10-10t14:34:22z”。我需要新的localdatetime对象的日期对象的年份,因为该对象将被设置为该日期对象的年份,并设置了特定的月、日和时间(yyyy-06-15t17:00:00z)。
从日期获取getyear()有1900个问题。
我通过 LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() 创建另一个对象以设置所需的月份和日期
创建localdatetime对象来设置时间
我觉得这是一个非常漫长的抽搐的方式,并想问是否有任何其他更短,更好的替代品。
编辑:
还有其他更短更好的选择吗?

ou6hu8tu

ou6hu8tu1#

因为日期时间字符串包含时区偏移信息。所以,你可以把它解析成 OffsetDateTime 对象,然后从中获取年份。

import java.time.LocalDateTime;
import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2015-10-10T14:34:22Z";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);
        System.out.println(odt.getYear());

        // If you want to get LocalDateTime from OffsetDateTime
        LocalDateTime ldt = odt.toLocalDateTime();
        System.out.println(ldt);
    }
}

输出:

2015-10-10T14:34:22Z
2015
2015-10-10T14:34:22

请注意 Z 在日期时间字符串中表示 Zulu 日期时间,并指定时区偏移 +00:00 小时或日期时间 UTC .
从日期获取getyear()有1900个问题。
api的日期时间 java.util 以及它们的格式化api, SimpleDateFormat 过时且容易出错。我建议您完全停止使用它们,转而使用现代的日期时间api。在trail:date-time了解有关现代日期时间api的更多信息。
如果您正在为一个android项目工作,并且您的android api级别仍然不符合java-8,请检查通过desugaring提供的java8+api以及如何在android项目中使用threetenabp。
从传统api转换为现代api:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDateTime = "2015-10-10T14:34:22Z";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        Date date = sdf.parse(strDateTime);
        Instant instant = date.toInstant();

        OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odt);
        System.out.println(odt.getYear());

        // If you want to get LocalDateTime from OffsetDateTime
        LocalDateTime ldt = odt.toLocalDateTime();
        System.out.println(ldt);
    }
}

输出:

2015-10-10T14:34:22Z
2015
2015-10-10T14:34:22

注意:如果要转换 Instant 进入 ZonedDateTimeUTC ,您可以按以下步骤操作:

ZonedDateTime zdt = instant.atZone(ZoneOffset.UTC);

或以下情况:

ZonedDateTime zdt = instant.atZone(ZoneId.of("Etc/UTC"));

请注意,三个字母的名称 ZoneId 容易出错,即避免使用 ZoneId.of("UTC") .

您的代码有什么问题:

您正在使用 .atZone(ZoneId.systemDefault()) 正在转换 Instant 对…的对象 ZonedDateTime 使用jvm的时区。你必须使用 .atOffset(ZoneOffset.UTC) 如上所示,保持日期时间具有相同的时区偏移(即。 +00:00 小时或日期时间 UTC )在日期时间字符串中。

jyztefdp

jyztefdp2#

试试这个:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
try {
    String s = "2015-10-10T14:34:22+02";
    s = s.replaceAll("T", " ");
    Date d = df.parse(s);
    Calendar cl = Calendar.getInstance();
    cl.setTime(d);
    System.out.println(cl.getTime());
    System.out.println("year : " + cl.get(Calendar.YEAR));
} catch (ParseException e) {
    System.err.println(e);
}

输出

Sat Oct 10 13:34:22 GMT+01:00 2015
year : 2015
ui7jx7zq

ui7jx7zq3#

时区至关重要

你需要决定你想要在哪个时区过一年。新年不是在全球某个时间点发生的,而是在大约26个小时的时间跨度内发生的。因此,如果您的日期字符串在新年的一天左右,如果您没有选择正确的时区,您的结果可能会偏离一年。例如:

ZoneId zone = ZoneId.of("America/Louisville");
    // The year in the following object does not matter
    ZonedDateTime fixedTimeOfYear = ZonedDateTime.of(2020, 6, 15, 17, 0, 0, 0, zone);

    String inputString = "2015-01-01T01:02:03Z";
    OffsetDateTime input = OffsetDateTime.parse(inputString);
    int year = input.atZoneSameInstant(zone).getYear();
    System.out.format("Year in %s is %d%n", zone, year);
    ZonedDateTime desiredTime = fixedTimeOfYear.withYear(year);
    System.out.println("Result: " + desiredTime);

此代码段的输出是:

Year in America/Louisville is 2014
Result: 2014-06-15T17:00-04:00[America/Louisville]

您注意到,尽管字符串中的年份是2015年,但在我为演示选择的时区中,仍然只有2014年,因此生成的日期和时间是2014年。我举了一个例子来说明我的观点。

不使用localdatetime

频繁使用 LocalDateTime 你在评论中提到的是误解。例如,对于2015年已知时区中的日期和时间, LocalDateTime 是错误的类。使用 ZonedDateTime 或者至少 OffsetDateTime 所以我们知道我们在说什么。这些类的优点是它们跟踪时区或自己偏移,并且它们定义了一个明确的时间点。 LocalDateTime 我什么都不做。

2o7dmzc5

2o7dmzc54#

也许这种方法可以帮助:

import java.text.ParseException;
import java.time.*;
import java.util.Date;

public class ConvertDate {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime();
        System.out.println(localDateTime);
        System.out.println(localDateTime.getYear());
    }
}

相关问题