需要帮助尝试格式化日期时间。

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

Needing help trying to Format DateTime

问题

以下是要翻译的内容:

所以,对于我的任务,我被要求为Shopify集成创建单元测试。我的一个断言方法需要我以特定的方式格式化日期。

我的断言方法如下,以下是跟踪信息。尝试跟上文档真的很困难。

assertEquals((new Date(2020, 7, 23)), order.getCreatedAt());
    
java.lang.AssertionError: 期望值Mon Aug 23 00:00:00 EDT 3920实际值2020-07-23T11:47:45.000-04:00
英文:

So for my assignemtn,I am instructed to create unit tests for Shopify integration. One of my assert methods require me to format the date a certain way.

My assert method is this and the following trace is as follows. It's really difficult trying to keep up with the documentations.

assertEquals((new Date(2020, 7, 23)),order.getCreatedAt());

java.lang.AssertionError: expected:<Mon Aug 23 00:00:00 EDT 3920> but was:<2020-07-23T11:47:45.000-04:00>

答案1

得分: 1

我建议您从过时且容易出错的 java.util.Date 切换到现代日期时间 API

您的代码出了什么问题:

java.util.Date 将第一个月视为 0,这意味着它将 7 视为 August(八月)。此外,它将 1900 添加到参数 year 中,这意味着对于参数值为 2020,它将返回年份设置为 3920 的对象。希望这足以让您理解 java.util.Date 设计有多糟糕。

使用现代日期时间 API:

您可以按照以下方式进行操作:

OffsetDateTime testData = OffsetDateTime.of(LocalDateTime.of(2020, Month.JULY, 23, 11, 47, 45, 0),
                    ZoneOffset.ofHours(-4));
assertEquals(testData, order.getCreatedAt());

这基于假设 order.getCreatedAt() 返回一个 OffsetDateTime 对象。请注意,您可以使用 7 代替 Month.JULY,但后者是表达月份值的惯用方式。

如果 order.getCreatedAt() 返回 2020-07-23T11:47:45.000-04:00 作为字符串,您可以如下所示将其解析为 OffsetDateTime

import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        // 将给定的日期时间字符串解析为 OffsetDateTime
        OffsetDateTime orderCreatedAt = OffsetDateTime.parse("2020-07-23T11:47:45.000-04:00");

        // 创建测试数据
        OffsetDateTime testData = OffsetDateTime.of(LocalDateTime.of(2020, Month.JULY, 23, 11, 47, 45, 0),
                ZoneOffset.ofHours(-4));

        // 显示
        System.out.println(orderCreatedAt);
        System.out.println(testData);

        // 断言
        //assertEquals(testData, orderCreatedAt);
    }
}

输出:

2020-07-23T11:47:45-04:00
2020-07-23T11:47:45-04:00

从**Trail: Date Time**了解更多关于现代日期时间 API 的信息。

英文:

I suggest you switch from the outdated and error-prone java.util.Date to the modern date-time API.

What went wrong with your code:

java.util.Date considers the first month as 0 which means 7 stands for August with it. Also, it adds 1900 to the parameter, year which means that for 2020 as the value of this parameter, it will give you an object with the year set as 3920. I hope, this is enough to understand how horribly java.util.Date has been designed.

Using the modern date-time API:

You can do it as follows:

OffsetDateTime testData = OffsetDateTime.of(LocalDateTime.of(2020, Month.JULY, 23, 11, 47, 45, 0),
				ZoneOffset.ofHours(-4));
assertEquals(testData, order.getCreatedAt());

This is based on the assumption that order.getCreatedAt() returns an object of OffsetDateTime. Note that you can use, 7 instead of Month.JULY but the later is the idiomatic way of expressing the value of the month.

If order.getCreatedAt() returns 2020-07-23T11:47:45.000-04:00 as String, you can parse it to OffsetDateTime as shown below:

import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
	public static void main(String[] args) {
		// Parse the given date-time string to OffsetDateTime
		OffsetDateTime orderCreatedAt = OffsetDateTime.parse("2020-07-23T11:47:45.000-04:00");

		// Create test data
		OffsetDateTime testData = OffsetDateTime.of(LocalDateTime.of(2020, Month.JULY, 23, 11, 47, 45, 0),
				ZoneOffset.ofHours(-4));

		// Display
		System.out.println(orderCreatedAt);
		System.out.println(testData);

		// Assert
		//assertEquals(testData, orderCreatedAt);
	}
}

Output:

2020-07-23T11:47:45-04:00
2020-07-23T11:47:45-04:00

Learn more about modern date-time API from Trail: Date Time.

huangapple
  • 本文由 发表于 2020年7月31日 23:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63195035.html
匿名

发表评论

匿名网友

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

确定