英文:
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.Date
和SimpleDateFormat
,改用现代日期时间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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论