与Java约会
Heather ZabriskieUnsplash上的照片

Java 8在所有合适的位置提供了所有信息流,只有我的一杯咖啡。 您知道吗,我想我会把它取出来进行数字约会。 CEST 15:00之后的8月4日,星期五。

我们见到多久?

不用担心,我会解决的。 让我看看要使用的类。

日期

那是个好名字! 我们来看一下。

Date

是的,我需要说清楚。 让我们一起去吧。

>>> Date date = new  Date();

一起来三看所有这些日期,这将是很棒的。

>>> System. out .println( date );
Fri Aug 04 15:10:50 CEST 2017

嘿,时区。 那个是从哪里来的?

public String toString ()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
where:
<snip>
zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.

好的,那不是答案。 Stackoverflow,您是我们唯一的希望。

TimeZone. getDefault ()

嗯,那么转换只是为了显示呢?

>>> System. out .println( date .getHours());
15

什么?

与Java约会

让我们看一下文档。

 @Deprecated 
public int getHours ()
Deprecated. As of JDK version 1.1, replaced by Calendar. get (Calendar.HOUR_OF_DAY).

哦当然了 不用担心,我会去看看这个日历。

日历

Calendar cal = Calendar.getInstance();

当然,实例化是如此近十年。 Singletons就在这里! 我确信他们虽然考虑了线程安全性。 让我们问一下Stack Overflow以确保。

Is java.util.Calendar thread safe or not? - Stack Overflow
https://stackoverflow.com/questions/12131324/is-java-util-calendar-thread-safe-or-not
Aug 26, 2012 - If you read the code you will see that none of the instance methods are synchronized, and none of the instance fields are volatile . ... In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.

哦。 好。 没关系,对我来说这并不重要。 让我们去看看我们能做什么!

 Getting and Setting Calendar Field Values 
The calendar field values can be set by calling the set methods.

当然,设置方法。 像setHours和setMonth一样,对不对?

public void set (int field, int value)
Sets the given calendar field to the given value.

嗯,那你怎么用呢?

cal.set(Calendar. MINUTE , 20);

当然,因为拥有类型系统的最大优点是可以显式忽略它。 但这可能只是底层API,我敢肯定这不是我们所拥有的。

public abstract void add (int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

public abstract void roll (int field, boolean up)
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

public void roll (int field, int amount)
Adds the specified (signed) amount to the specified calendar field without changing larger fields. A negative amount means to roll down.

这是哪种API? 你在说什么? 为什么要滚动日期,“滚动”到底意味着什么? 仅当您在山上时,此类课程才有意义吗?

我们是否定期检查设计此产品的人? 因为我不确定他是否会好起来。

在与我相关的水平上,为什么我没有实际要做的事情? 为什么我不能去下周,或者一天的开始或结束? 为什么每次我与您一起工作时,为什么期望我自己实现所有这些功能?

Java,您是在想我。

与Java约会
Java日期/时间库的设计者。

Java 8

我显然落后于时代。 Java 8是它的所在地,它具有全新的日期/时间API。 我们得到什么?

The first classes you will probably encounter when using the new API are LocalDate and LocalTime.

好吧,让我们看看。

class LocalDateTime
A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

好的,没有时区。 因此,这意味着我想查看日期时必须提供它?

>>> LocalDateTime timePoint = LocalDateTime.now();
>>> System. out .println( timePoint );
2017-08-04T15:23:41.541

嘿,那是我的时区。 但是它没有时区信息。

public static LocalDateTime now ()
Obtains the current date-time from the system clock in the default time-zone.

所以你说...

>>> System. out .println( timePoint. getHour () );
15
与Java约会

让我们看看文档对此要说些什么。

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

所以这是一个花哨的字符串? 我的意思是,没有时区的时间毫无意义。 您总是有一个时区,即使您假装没有,我们也已经看到了它的发生。 为什么不总是强迫用户明确? 什么时候使用默认时区更好? 为什么还要添加一些“本地”和挂钟的概念并隐藏实际上并不那么困难的东西呢?

我可以使用一些简单明了的东西吗?

 Class Instant 
An instantaneous point on the time-line.
This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.

我谨慎乐观...

>>> Instant instant = Instant.now();
>>> System. out .println( instant );
2017-08-04T15:28:01.354Z

默认情况下,UTC对我来说很有意义。 这是有希望的! 只是时间线上的一个简单,可预测的值,始终使用UTC,因此我可以在需要显示给用户时简单地进行转换。 而且我敢肯定,他们至少实现了比Calendar更好的API…

public int get (TemporalField field)
Gets the value of the specified field from this instant as an int.
public Instant plus (long amountToAdd, TemporalUnit unit)
Returns a copy of this instant with the specified amount added.

您知道什么是Java,我想我会留在家里玩Python。

并不全是坏事

让我们退后一步,因为LocalDateTime的API实际上非常不错。

LocalDateTime
LocalDateTime
This method adds the specified amount in weeks to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.
public LocalDateTime withHour (int hour)
Returns a copy of this LocalDateTime with the hour-of-day altered.

还有一个使时区更加明确的类,它实现了相同的API。

ZonedDateTime is a date and time with a fully qualified time zone. This can resolve an offset at any point in time.
2007-12-03T10:15:30+01:00 Europe/Paris

让我们尝试一下!

>>> ZonedDateTime timePoint = ZonedDateTime.now();
>>> System. out .println( timePoint );
2017-08-04T15:33:02.325+02:00[Europe/Amsterdam]
>>> System. out .println( timePoint. getHour());
15
>>> timePoint = timePoint.of(1983, JULY.getValue(), 20, 15, 27, 40, 0, ZoneId.of("Europe/London"));
>>> System. out .println( timePoint );
1983-07-20T15:27:40+01:00[Europe/London]
>>> timePoint = timePoint. plusYears(30).minusHours(4)
>>> System. out .println( timePoint );
2013-07-20T11:27:40+01:00[Europe/London]

这是显式的,实用的并且相当优雅。 该API是流利的,这使得设置特定日期或执行多次修改更加民俗化。 而且那里也没有惊喜。

奇怪的是,前面讨论的类通常在到达这一类之前就已进行了描述,并且很多焦点都集中在LocalX类上。 但我建议在大多数情况下只使用ZonedDateTime。 通常,在处理日期和时间时,最佳做法是在内部将所有内容明确地视为UTC。 然后,仅在应用程序中必须显示日期的时间点,将日期时间转换为适用于特定用户的时区。 ZonedDateTime可以很好地促进这种模式。

这篇文章的内容主要是试图讽刺,并不能准确反映作者的思维过程或编程能力。 实际上,涉及更多的咒骂。

From: https://hackernoon.com/going-on-a-date-with-java-9bdac2c950b3

相关文章: