如何在 Mirth 中将本地日期转换为 UTC?

How to convert local date to UTC in Mirth?

在 Mirth 中,我收到一个本地日期时间字符串 (201801011000),我需要将其转换为 UTC。我很快发现使用经典的 js new Date() 效果不佳。

例如:

var d = new Date("2018-01-01 10:00");
logger.info(d.toString());

给我一个 Invalid Date

所以经过更多搜索,我发现我可以这样做:

var d = DateUtil.getDate("yyyyMMddHHmm", "201801011000");

从这里我被困住了。我不知道如何将其转换为 UTC。假设本地服务器时区现在已经足够了,但将来我还需要设置一个特定的非本地时区。

我试图获得可以与 Object.getOwnPropertyNames(d) 一起使用的方法,但这对我很有帮助 TypeError: Expected argument of type object, but instead had type object

我也尝试查找 DateUtil 的 java 文档并尝试了一些方法,但没有任何效果。

有人知道如何将日期字符串从本地时间转换为 UTC 时间吗?欢迎所有提示!

点个赞

var d = DateUtil.getDate("yyyyMMddHHmm", "201801011000");
var utcD = new Date(d).toISOString();

编辑:关于 .toISOString() 的信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

好的,折腾了整整两天我终于找到了解决办法。最后我不得不使用 Java,但由于我无法导入任何 java 依赖项,我不得不使用它们的直接 class 路径(例如:java.text.SimpleDateFormat)。

最终这对我有用:

var datestr = "201207011000".slice(0, 12);  // This is just a datetime string to test with

var formatter_hl7 = new java.text.SimpleDateFormat("yyyyMMddHHmm");
formatter_hl7.setTimeZone(java.util.TimeZone.getTimeZone("CET"));
var formatter_utc = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
formatter_utc.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));

var date_in_utc = formatter_utc.format(formatter_hl7.parse(date_str));

无论如何,祝大家有个美好的一天!

在我的项目中有将 datestring 本地时间转换为 UTC 的功能,

function getDateInUTC(dateString) {
    return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm").setTimeZone(java.util.TimeZone.getTimeZone("UTC")).format(new java.text.SimpleDateFormat("yyyyMMddHHmm").setTimeZone(java.util.TimeZone.getTimeZone("CET")).parse(dateString));
}

尽情享受吧:)

您应该使用 Java8 提供的最新 类 java.time

步骤如下:

Step-1. 解析 StringLocalDateTime

Step-2.LocalDateTime转换为ZonedDateTime,然后我们就可以在不同的timezone.

之间进行转换了

希望对您有所帮助:

在欢乐中你可以这样写:

String str = "201207011000"; 
var date_in_utc =java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
                .format(java.time.ZonedDateTime.of(java.time.LocalDateTime
                .parse(str,java.time.format.DateTimeFormatter
                .ofPattern("yyyyMMddHHmm")),java.time.ZoneId.of("CET"))
                .withZoneSameInstant(java.time.ZoneOffset.UTC));

完整代码段:

ZoneId cet = ZoneId.of("CET");
String str = "201207011000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter);
ZonedDateTime dateAndTimeInCET = ZonedDateTime.of(localtDateAndTime, cet );
System.out.println("Current date and time in a CET timezone : " + dateAndTimeInCET);
ZonedDateTime utcDate = dateAndTimeInCET.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("Current date and time in UTC : " + utcDate);
System.out.println("Current date and time in UTC : " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(utcDate));

tl;博士

  • 不要使用 DateUtil 无论如何。 (也许是 Apache DateUtils 库?)
  • 不要使用糟糕的旧日期时间类,例如java.util.Date
  • 使用现代行业领先java.time类.

用于解析缺少偏移量的字符串,然后为 UTC 本身分配零偏移量的代码。

LocalDateTime                // Represents a date and a time-of-day but without any concept of time zone or offset-from-UTC. NOT a moment, NOT a point on the timeline.
.parse( 
    "201801011000" ,
     DateTimeFormatter.ofPattern( "uuuuMMddHHmm" )
)  
.atOffset( ZoneOffset.UTC )  // Assign an offset-from-UTC. Do this only if you are CERTAIN this offset was originally intended for this input but was unfortunately omitted from the text. Returns an `OffsetDateTime`.
.toInstant()                 // Extract an `Instant` from the `OffsetDateTime`. Basically the same thing. But `Instant` is always in UTC by definition, so this type is more appropriate if your intention is to work only in UTC. On the other hand, `Instant` is a basic class, and `OffsetDateTime` is more flexible such as various formatting patterns when generating `String` object to represent its value.

使用java.time

Java 中的现代方法使用 java.time 类。这个行业领先的框架取代了非常麻烦的旧日期时间 类,例如 DateCalendarSimpleDateFormat

DateTimeFormatter

解析您的输入字符串。定义要匹配的格式模式。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) ;
String input = "201801011000" ;

LocalDateTime

解析为 LocalDateTime,因为您的输入缺少时区指示符或与 UTC 的偏移量。

LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

缺少区域或偏移量意味着这不是代表一个时刻,不是时间轴上的一个点。相反,这表示 潜在 个时刻,时间跨度约为 26-27 小时,即全球时区范围。

OffsetDateTime

如果您确定该日期和时间旨在表示 UTC 中的某个时刻,请应用常量 ZoneOffset.UTC 以获得 OffsetDateTime 对象。

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

ZonedDateTime

你的问题含糊不清。听起来您可能知道用于此输入的特定时区。如果是这样,赋值一个ZoneId得到一个ZonedDateTime对象。

了解与 UTC 的偏移量只是小时数、分钟数和秒数。仅此而已。相比之下,时区要多得多。时区是某个地区的人们使用的偏移量的过去、现在和未来变化的历史。

指定 proper time zone name in the format of continent/region, such as America/Montreal, Africa/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们 不是 真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

Instant

调整回 UTC 的快速方法是提取 Instant 对象。 Instant 始终采用 UTC。

Instant instan = zdt.toInstant() ;

ISO 8601

提示:不要使用自定义格式将日期时间值作为文本交换,而应仅使用标准 ISO 8601 格式。标准格式实用,易于机器解析,易于跨文化的人类阅读。

java.time类在parsing/generating字符串时默认使用ISO 8601格式。 ZonedDateTime::toString 方法明智地扩展了标准,将区域名称附加在方括号中。

Instant instant = Instant.parse( "2018-07-23T16:18:54Z" ) ;  // `Z` on the end means UTC, pronounced “Zulu”. 
String output = instant.toString() ;  // 2018-07-23T16:18:54Z

并且始终在您的字符串中包含偏移量和时区。暂时省略 offset/zone 就像在价格中省略货币:您所剩下的只是一个毫无价值的模棱两可的数字。实际上,总比没有更糟糕,因为它会导致各种混乱和错误。


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

在哪里获取java.time类?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.