原文链接:https:mp。weixin。qq。comseROOtH6vsbrpW8Oy1Fhew 大家好,我是团长。 今天我们将聊聊如何在Java中把一个Instant格式化为一个字符串。我们将展示如何使用Java原生和第三方库(如JodaTime)来处理这个事情。使用Java原生格式化Instant 在Java8中有个名为Instant类。通常情况下,我们可以使用这个类来记录我们应用程序中的事件时间戳。 让我们看看如何把它转换成一个字符串对象。使用DateTimeFormatter类 一般来说,我们将需要一个格式化器来格式化一个即时对象。Java8引入了DateTimeFormatter类来统一格式化日期和时间。 DateTimeFormatter提供了format()方法来完成这项工作。 简单地说,DateTimeFormatter需要一个时区来格式化一个Instant。没有它,它将无法将Instant转换为人类可读的日期时间域。 例如,让我们假设我们想用dd。MM。yyyy格式来显示我们的即时信息实例。publicclassFormatInstantUnitTest{privatestaticfinalStringPATTERNFORMATdd。MM。TestpublicvoidgivenInstantwhenUsingDateTimeFormatterthenFormat(){DateTimeFormatterformatterDateTimeFormatter。ofPattern(PATTERNFORMAT)。withZone(ZoneId。systemDefault());InstantinstantInstant。parse(20220421T15:35:24。00Z);StringformattedInstantformatter。format(instant);assertThat(formattedInstant)。isEqualTo(21。04。2022);}} 如上所示,我们可以使用withZone()方法来指定时区。 请记住,如果不能指定时区将导致UnsupportedTemporalTypeException。Test(expectedUnsupportedTemporalTypeException。class)publicvoidgivenInstantwhenNotSpecifyingTimeZonethenThrowException(){DateTimeFormatterformatterDateTimeFormatter。ofPattern(PATTERNFORMAT);InstantinstantInstant。now();formatter。format(instant);}使用toString()方法 另一个解决方案是使用toString()方法来获得即时对象的字符串表示。 让我们用一个测试案例举例说明toString()方法的使用。TestpublicvoidgivenInstantwhenUsingToStringthenFormat(){InstantinstantInstant。ofEpochMilli(1641828224000L);StringformattedInstantinstant。toString();assertThat(formattedInstant)。isEqualTo(20220110T15:23:44Z);} 这种方法的局限性在于,我们不能使用自定义的、对人友好的格式来显示即时信息。JodaTime库 另外,我们也可以使用JodaTimeAPI来实现同样的目标。这个库提供了一套随时可用的类和接口,用于在Java中操作日期和时间。 在这些类中,我们发现DateTimeFormat类。顾名思义,这个类可以用来格式化或解析进出字符串的日期时间数据。 因此,让我们来说明如何使用DateTimeFormatter来将一个瞬间转换为一个字符串。TestpublicvoidgivenInstantwhenUsingJodaTimethenFormat(){org。joda。time。Instantinstantneworg。joda。time。Instant(20220320T10:11:12);StringformattedInstantDateTimeFormat。forPattern(PATTERNFORMAT)。print(instant);assertThat(formattedInstant)。isEqualTo(20。03。2022);} 我们可以看到,DateTimeFormatter提供forPattern()来指定格式化模式,print()来格式化即时对象。总结 在这篇文章中,我们了解了如何在Java中把一个Instant格式化为一个字符串。 在这一过程中,我们了解了一些使用Java原生方法来实现这一目标的方法。然后,我们解释了如何使用JodaTime库来完成同样的事情。