Converting String Date to DateTimePST(String) and DateTimeLocal(String) using Offset?

huangapple go评论120阅读模式
英文:

Converting String Date to DateTimePST(String) and DateTimeLocal(String) using Offset?

问题

我有三个参数,UTCTime以及两个偏移量,所有三个参数都是字符串,我需要进行以下转换:

输入:

String UTCTime = "2020-09-29 16:06:00";
String PSTOffset = "-07:00";
String LocalOffset = "-04:00";

预期输出:

LocalTime = "2020-09-29 12:06:00";
PSTTime = "2020-09-29 09:06:00";

我尝试了类似以下的方法:

OffsetDateTime OffsetLocalTime = OffsetDateTime.parse(String.format("%sT%sZ", UTCTime.split(" ")[0], UTCTime.split(" ")[1]));
OffsetLocalTime = OffsetLocalTime.withOffsetSameInstant(ZoneOffset.of(LocalOffset));

OffsetDateTime OffsetPSTTime = OffsetDateTime.parse(String.format("%sT%sZ", UTCTime.split(" ")[0], UTCTime.split(" ")[1]));
OffsetPSTTime = OffsetPSTTime.withOffsetSameInstant(ZoneOffset.of(PSTOffset));

String LocalTime = String.format("%s %s", OffsetLocalTime.toString().substring(0, OffsetLocalTime.toString().lastIndexOf('-')).split("T")[0], OffsetLocalTime.toString().substring(0, OffsetLocalTime.toString().lastIndexOf('-')).split("T")[1]);
String PSTTime = String.format("%s %s", OffsetPSTTime.toString().substring(0, OffsetPSTTime.toString().lastIndexOf('-')).split("T")[0], OffsetPSTTime.toString().substring(0, OffsetPSTTime.toString().lastIndexOf('-')).split("T")[1]);

System.out.println("OffsetLocalTime: " + OffsetLocalTime);
System.out.println("OffsetPSTTime: " + OffsetPSTTime);

System.out.println("LocalTime: " + LocalTime);
System.out.println("PSTTime: " + PSTTime);

实际输出:

OffsetLocalTime: 2020-09-29T12:06-04:00
OffsetPSTTime: 2020-09-29T09:06-07:00
LocalTime: 2020-09-29 12:06
PSTTime: 2020-09-29 09:06

我遗漏了秒,并且这种方法似乎效率较低,因为我不需要输出中的 'T' 或偏移量,并且需要将它们去除。我不确定是否已经有人提过这个问题。非常感谢您的帮助。

英文:

I have three parameters, UTCTime and two offsets all three as strings, I need to convert them as below

Input: 
    String UTCTime = "2020-09-29 16:06:00";
    String PSTOffset = "-07:00";
    String LocalOffset = "-04:00";

Output Expected:
        LocalTime = "2020-09-29 12:06:00"
        PSTTime = "2020-09-29 09:06:00"

I tried doing something like this

    OffsetDateTime OffsetLocalTime = OffsetDateTime.parse(String.format("%sT%sZ",UTCTime.split(" ")[0],UTCTime.split(" ")[1]));
    OffsetLocalTime = OffsetLocalTime.withOffsetSameInstant(ZoneOffset.of(LocalOffset));

    OffsetDateTime OffsetPSTTime = OffsetDateTime.parse(String.format("%sT%sZ",UTCTime.split(" ")[0],UTCTime.split(" ")[1]));
    OffsetPSTTime = OffsetPSTTime.withOffsetSameInstant(ZoneOffset.of(PSTOffset));

    String LocalTime = String.format("%s %s",OffsetLocalTime.toString().substring(0,OffsetLocalTime.toString().lastIndexOf('-')).split("T")[0],OffsetLocalTime.toString().substring(0,OffsetLocalTime.toString().lastIndexOf('-')).split("T")[1]);
    String PSTTime = String.format("%s %s",OffsetPSTTime.toString().substring(0,OffsetPSTTime.toString().lastIndexOf('-')).split("T")[0],OffsetPSTTime.toString().substring(0,OffsetPSTTime.toString().lastIndexOf('-')).split("T")[1]);

    System.out.println("OffsetLocalTime: "+OffsetLocalTime);
    System.out.println("OffsetPSTTime: "+ OffsetPSTTime);

    System.out.println("LocalTime: "+ LocalTime);
    System.out.println("PSTTime: "+ PSTTime);

    Actual Output:

    OffsetLocalTime: 2020-09-29T12:06-04:00
    OffsetPSTTime: 2020-09-29T09:06-07:00
    LocalTime: 2020-09-29 12:06
    PSTTime: 2020-09-29 09:06

I am missing out seconds and this seems to be an inefficient way beacuse i do not need 'T' or offset in my output and i am getting rid of them. I am not sure if this question is already asked. Any help is appreciated.

答案1

得分: 2

自从给定的日期时间字符串2020-09-29 16:06:00没有区偏移信息,首先需要将其解析为LocalDateTime,然后可以将LocalDateTime转换为OffsetDateTime

请按以下方式执行:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

        // 给定的日期时间
        String localTimeUTC = "2020-09-29 16:06:00";

        // 从给定的日期时间字符串获取OffsetDateTime
        OffsetDateTime odtUTC = LocalDateTime.parse(localTimeUTC, formatter).atOffset(ZoneOffset.UTC);

        // 带有偏移量-7小时的OffsetDateTime
        OffsetDateTime odtAtOffsetMinus7Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-7));
        System.out.println(odtAtOffsetMinus7Hours.format(formatter));

        // 带有偏移量-4小时的OffsetDateTime
        OffsetDateTime odtAtOffsetMinus4Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-4));
        System.out.println(odtAtOffsetMinus4Hours.format(formatter));
    }
}

输出:

2020-09-29 09:06:00
2020-09-29 12:06:00

注意: 可以根据实际情况,使用ZoneOffset.of("-07:00")来代替ZoneOffset.ofHours(-7),具体取决于你手头有什么样的数字或字符串。

英文:

Since the given date-time string, 2020-09-29 16:06:00 does not have zone-offset information, it needs to be first parsed into LocalDateTime and then the LocalDateTime can be converted into OffsetDateTime.

Do it as follows:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {
	public static void main(String[] args) {
		// Define the formatter
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

		// Given date-time
		String localTimeUTC = "2020-09-29 16:06:00";

		// Get OffsetDateTime from the given date-time string
		OffsetDateTime odtUTC = LocalDateTime.parse(localTimeUTC, formatter).atOffset(ZoneOffset.UTC);

		// OffsetDateTime with an Zone-Offset of -7 hours
		OffsetDateTime odtAtOffsetMinus7Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-7));
		System.out.println(odtAtOffsetMinus7Hours.format(formatter));

		// OffsetDateTime with an Zone-Offset of -4 hours
		OffsetDateTime odtAtOffsetMinus4Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-4));
		System.out.println(odtAtOffsetMinus4Hours.format(formatter));
	}
}

Output:

2020-09-29 09:06:00
2020-09-29 12:06:00

Note: Instead of using ZoneOffset.ofHours(-7), you can also use ZoneOffset.of("-07:00") depending on what (number/string) you have got to work with.

huangapple
  • 本文由 发表于 2020年10月2日 00:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64159696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定