Gatling Scala 数据随机格式不正确

Gatling Scala date randomly not well formated

我正在尝试使用 Gatling 和 scala pluging(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"));

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

如果您必须使用日历 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)
 }

I tried to add

format.setTimeZone(TimeZone.getTimeZone("UTC"));

when I saw the problem for the first time but it has no effect.

无论你添加什么time-zone,结果总是以Z结尾;仅此而已(即永远不会像您提到的那样 Z02:00 ),直到您在此之后明确添加其他内容。原因是您将 Z 添加为文字 ('Z'),因此它将始终打印为 Z 而不是其他内容。

如果你的模式如下:

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

不同时区的结果会有所不同,因为在此模式中,Z 指定 Zone-Offset 而不是文字 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

我建议您从过时的 error-prone java.util date-time API 和 SimpleDateFormat 切换到 modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.

使用现代date-time 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