加特林Scala日期随机格式不正确

问题描述

我正在尝试使用Gatling和scala插入(https://github.com/jeanadrien/gatling-mqtt-protocol)来做一个数据生成器。生成的数据包括日期。

这是相关代码:

val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
format.setTimeZone(TimeZone.getTimeZone("UTC"));

  val publish = repeat(repeatTimes) {
   feed(feeder)
    .exec(mqtt("publish")
    .publish(format.format(Calendar.getInstance().getTime( )),QoS.AT_LEAST_ONCE,retain = false))
    .pause(pauseTime)
 }

我的问题是,大约90%的生成日期都采用这种格式:

2020-09-02T17:06:48Z

大约10%的生成日期是这种格式:

2020-09-02T17:06:48Z02:00

我只想要第一种格式。我试图添加

format.setTimeZone(TimeZone.getTimeZone(“ UTC”));

当我第一次看到这个问题,但是没有效果。

解决方法

如果必须使用Calender API,则可以按照@Arvind的回答进行操作,但是我建议您使用较新的日期时间API。

在格式化程序上设置时区仅对缺少时区信息的时态有用。对于时区感知时态,您需要将其转换为所需时区。

val dateTimeFormat = DateTimeFormatter.ISO_DATE_TIME

  val publish = repeat(repeatTimes) {
   feed(feeder)
    .exec(
      mqtt("publish")
        .publish(
          ZonedDateTime.now().withZoneSameInstant(ZoneId.of("UTC")),QoS.AT_LEAST_ONCE,retain = false
        )
    )
    .pause(pauseTime)
 }
,

我尝试添加

format.setTimeZone(TimeZone.getTimeZone(“ UTC”));

当我第一次看到这个问题,但是没有效果。

无论您添加什么时区,结果始终以Z结尾;除非您在此之后明确添加其他内容,否则别无其他(例如,您从未提到过Z02:00)。原因是您要添加Z作为文字('Z'),因此它将始终打印为Z,而没有其他显示。

您的模式如下:

yyyy-MM-dd'T'HH:mm:ssZ

对于不同的时区,结果可能会有所不同,因为在这种模式下,Z指定区域偏移,而不是文字Z(即'Z'),例如

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(format.format(Calendar.getInstance().getTime()));
        
        format.setTimeZone(TimeZone.getTimeZone("Europe/London"));
        System.out.println(format.format(Calendar.getInstance().getTime()));
        
        format.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
        System.out.println(format.format(Calendar.getInstance().getTime()));
    }
}

输出:

2020-09-02T21:15:36+0000
2020-09-02T22:15:36+0100
2020-09-03T02:45:36+0530

我建议您从过时且容易出错的java.util日期时间API和SimpleDateFormat切换到createFromAsset() java.time日期时间API和相应的格式化API (软件包java.time.format)。从 modern 了解有关现代日期时间API的更多信息。

使用现代的日期时间API:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Instant instant = Instant.now();

        // Printing default pattern (i.e. whatever Instant#toString returns)
        System.out.println(instant);

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

        // Print custom formats
        System.out.println(zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX")));
        System.out.println(zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")));
        System.out.println(zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz")));
    }
}

输出:

2020-09-02T21:30:36.770160Z
2020-09-02T21:30:36Z
2020-09-02T21:30:36+0000
2020-09-02T21:30:36UTC

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...