java.time.LocalDate与Instant的“营业日期" [英] java.time.LocalDate vs Instant for a 'business date'

查看:120
本文介绍了java.time.LocalDate与Instant的“营业日期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想代表一个营业日",例如发生在"2019年6月3日"的交易.我们完全为此目的而积极地忽略时区,因为我们充分了解到日本的"2019年6月3日"可能是美国的"2019年6月2日",因此天"内的订购同样无关紧要.所有日期都是今天或以前的日期.

I want to represent a 'business date', eg a transaction that happened 'on 3 June 2019'. We actively ignore timezones for this purpose, in full knowledge that 'on 3 June 2019' in Japan might be 'on 2 June 2019' in the US - and ordering within the 'day' is equally irrelevant. All dates will be today, or prior dates.

我的明显答案是这是一个LocalDate.但是,有人建议将其更好地表示为2019-06-03T00:00:00.000ZInstant.

My obvious answer is that this is a LocalDate. However someone else has suggested this would be better represented as an Instant of 2019-06-03T00:00:00.000Z.

除了在UI中将其转换/格式化为人类可读的日期时需要进行的各种调用之外,这两种方法之间实际上是否存在差异?

Apart from the different calls we'll have to make when converting/formatting this to human-readable dates in a UI, are there actually any differences between the two approaches?

Instant和LocalDateTime有什么区别?,因为时间与该问题无关,并且仅与过去(或当前)日期有关.

This is a different question to What's the difference between Instant and LocalDateTime? because time is irrelevant to this question, and it only relates to past (or current) dates.

推荐答案

我曾经在一个产品上工作过,在该产品上,我们错误地将生日表示为即时日期.

I once worked on a product where we made the mistake of representing a date of birth as an instant.

这是我们几年后仍在处理的微小"设计错误之一.

This was one of those "tiny" design errors that we were still dealing with years later.

问题在于,随着用户更改时区,您无法在UI中可靠地显示它;由于开发人员在服务器运行所在的不同时区工作,因此在后端进行转换很容易出错.它虽然可以在一个时区工作,但是随着产品扩展到其他时区,这完全让人头疼.

The problem was that you couldn't reliably show it in the UI as the user changed time zone; it was error-prone to convert on the backend, because of developers working in different time zones to where the server was running. It sort-of worked in a single time zone, but as the product was extended to other time zones, it just became a total headache.

时刻是时间轴上的一个;本地日期是时间的范围(而不是明确定义的时间范围,从某种意义上讲,它可以表示不同时区的不同时刻范围).它们代表不同的事物.

An instant is a point on the timeline; a local date is a range of times (and not a well-defined range of times, in the sense that it can represent different ranges of instants in different time zones). They represent different things.

如果要表示日期,请存储日期.我并不是说java.util.Date,这确实是一个瞬间.

If you want to represent a date, store a date. And I don't mean a java.util.Date, which is really an instant.

使用 LocalDate .

这篇关于java.time.LocalDate与Instant的“营业日期"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆