为什么在解析和格式化日期字符串后,Groovy会更改小时/分钟?

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

Why is groovy changing the hours/minutes after parsing and formatting date string?

问题

Method1和Method2在解析和格式化日期时似乎产生了不同的结果。以下是翻译好的部分:

Method1:

String record = data.getValue(4, 1) 
// 从SQL表中返回"2020-07-28 15:43:16.5168174 -05:00"
        
def formattedDate = Date.parse("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX", record).format("yyyy-MM-dd'T'HH:mm:ss.SSS")
log.logInfo("查询日期 = " + formattedDate)

返回结果为 2020-07-28T17:09:24.174

Method2:

def queriedDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX").parse(record)
log.logInfo("queriedDate2= " + queriedDate2)

返回结果为 Tue Jul 28 17:09:24 CDT 2020

有人可以帮我理解发生了什么情况吗?

英文:

I'm pulling a date string from a SQL table. The date string being pulled from Sql is...

2020-07-28 15:35:45.9375232 -05:00

and when I try and parse and format, it's returning a different time than what was passed in.

Method1:

String record = data.getValue(4, 1) 
// returns "2020-07-28 15:43:16.5168174 -05:00" from SQL table
    
def formattedDate = Date.parse("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX", record).format("yyyy-MM-dd'T'HH:mm:ss.SSS")
		log.logInfo("queried date = " + formattedDate)

Returns 2020-07-28T17:09:24.174

and Method2:

def queriedDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX").parse(record)
	log.logInfo("queriedDate2= " + queriedDate2)

Returns Tue Jul 28 17:09:24 CDT 2020

Can anyone help me understand what's going on?

答案1

得分: 1

可能SimpleDateFormat正在忽略给定日期时间字符串中的区偏移(-5小时),并在解析时应用您的JVM时区。我建议您切换到过时且容易出错的java.util.DateSimpleDateFormat,改用现代日期时间API来摆脱这种问题。

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

public class Main {
    public static void main(String[] args) {
        // 给定的日期时间字符串
        String dateTimeStr = "2020-07-28 15:35:45.9375232 -05:00";

        // 从给定的日期时间字符串中获取OffsetDateTime
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr,
                DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS XXX"));
        System.out.println(odt);

        // 您可以使用不同的DateTimeFormatter模式以不同的形式打印它,例如:
        System.out.println(DateTimeFormatter.ofPattern("MMM dd uuuu").format(odt));
        System.out.println(DateTimeFormatter.ofPattern("MMM dd uuuu HH:mm:ss.SSSSSSS ZZZZZ").format(odt));
        System.out.println(DateTimeFormatter.ofPattern("MM/uuuu/dd hh:mm:ss a").format(odt));
    }
}

输出:

2020-07-28T15:35:45.937523200-05:00
Jul 28 2020
Jul 28 2020 15:35:45.9375232 -05:00
07/2020/28 03:35:45 pm
英文:

Probably SimpleDateFormat is ignoring the zone-offset (-5 hours) in the give date-time string and applying your JVM's time-zone while parsing it. I suggest you switch from the outdated and error-prone java.util.Date and SimpleDateFormat to the modern date-time API to get rid of such problems.

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

public class Main {
	public static void main(String[] args) {
		// Given date-time string
		String dateTimeStr = "2020-07-28 15:35:45.9375232 -05:00";

		// OffsetDateTime from the given date-time string
		OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr,
				DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS XXX"));
		System.out.println(odt);

		// You can print it in different forms using DateTimeFormatter patterns e.g.
		System.out.println(DateTimeFormatter.ofPattern("MMM dd uuuu").format(odt));
		System.out.println(DateTimeFormatter.ofPattern("MMM dd uuuu HH:mm:ss.SSSSSSS ZZZZZ").format(odt));
		System.out.println(DateTimeFormatter.ofPattern("MM/uuuu/dd hh:mm:ss a").format(odt));
	}
}

Output:

2020-07-28T15:35:45.937523200-05:00
Jul 28 2020
Jul 28 2020 15:35:45.9375232 -05:00
07/2020/28 03:35:45 pm

答案2

得分: 0

Your new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX") expects a timezone, e.g. GMT for the XXX. Your returned value "2020-07-28 15:43:16.5168174 -05:00" seems to have a GMT offset: -05:00. I would guess that the .parse() is erroneous and returns some sort of default format. Because of that, it is suggested to use Arvind's suggested OffsetDateTime.

英文:

Your

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX")

expects a timezone, e.g. GMT for the XXX. Your returned value

"2020-07-28 15:43:16.5168174 -05:00"

seems to have a GMT offset: -05:00
I would guess that the .parse() is erroneous and returns some sort of default format.

Because of that, it is suggested to use Arvind's suggested OffsetDateTime.

huangapple
  • 本文由 发表于 2020年7月29日 05:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63142680.html
匿名

发表评论

匿名网友

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

确定