当前位置:网站首页>阿里面试官:聊聊如何格式化Instant
阿里面试官:聊聊如何格式化Instant
2022-08-03 03:25:00 【蒙娜丽莎的Java】
今天我们将聊聊如何在Java中把一个 Instant 格式化为一个字符串。我们将展示如何使用 Java 原生和第三方库(如Joda-Time)来处理这个事情。
使用 Java 原生格式化Instant
在 Java 8 中有个名为 Instant 类。通常情况下,我们可以使用这个类来记录我们应用程序中的事件时间戳。
让我们看看如何把它转换成一个字符串对象。
使用 DateTimeFormatter 类
一般来说,我们将需要一个格式化器来格式化一个即时对象。Java 8引入了DateTimeFormatter类来统一格式化日期和时间。
DateTimeFormatter 提供了 format() 方法来完成这项工作。
简单地说,DateTimeFormatter 需要一个时区来格式化一个 Instant 。没有它,它将无法将Instant 转换为人类可读的日期/时间域。
例如,让我们假设我们想用 dd.MM.yyyy 格式来显示我们的即时信息实例。
public class FormatInstantUnitTest
{
private static final String PATTERN_FORMAT = “dd.MM.yyyy”;
@Test public void givenInstant_whenUsingDateTimeFormatter_thenFormat()
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT) .withZone(ZoneId.systemDefault());
Instant instant = Instant.parse(“2022-04-21T15:35:24.00Z”);
String formattedInstant = formatter.format(instant); assertThat(formattedInstant).isEqualTo(“21.04.2022”);
}
}
如上所示,我们可以使用withZone()方法来指定时区。
请记住,如果不能指定时区将导致 UnsupportedTemporalTypeException。
@Test(expected = UnsupportedTemporalTypeException.class)
public void givenInstant_whenNotSpecifyingTimeZone_thenThrowException()
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
Instant instant = Instant.now();
formatter.format(instant);}
使用toString()方法
另一个解决方案是使用toString()方法来获得即时对象的字符串表示。
让我们用一个测试案例举例说明toString()方法的使用。
@Testpublic void givenInstant_whenUsingToString_thenFormat()
{
Instant instant = Instant.ofEpochMilli(1641828224000L);
String formattedInstant = instant.toString(); assertThat(formattedInstant).isEqualTo(“2022-01-10T15:23:44Z”);
}
这种方法的局限性在于,我们不能使用自定义的、对人友好的格式来显示即时信息。
Joda-Time库
另外,我们也可以使用 Joda-Time API 来实现同样的目标。这个库提供了一套随时可用的类和接口,用于在Java中操作日期和时间。
在这些类中,我们发现DateTimeFormat类。顾名思义,这个类可以用来格式化或解析进出字符串的日期/时间数据。
因此,让我们来说明如何使用DateTimeFormatter来将一个瞬间转换为一个字符串。
@Testpublic void givenInstant_whenUsingJodaTime_thenFormat()
{
org.joda.time.Instant instant = new org.joda.time.Instant(“2022-03-20T10:11:12”);
String formattedInstant = DateTimeFormat.forPattern(PATTERN_FORMAT) .print(instant); assertThat(formattedInstant).isEqualTo(“20.03.2022”);}
我们可以看到,DateTimeFormatter提供forPattern()来指定格式化模式,print()来格式化即时对象。
总结
在这篇文章中,我们了解了如何在Java中把一个 Instant 格式化为一个字符串。
在这一过程中,我们了解了一些使用Java 原生方法来实现这一目标的方法。然后,我们解释了如何使用Joda-Time库来完成同样的事情。
边栏推荐
- 第八章 字符输入输出和输入验证
- leetcode:151. 颠倒字符串中的单词
- Chinese valentine's day??To the liver is the way!!!!!Auto. Js special position control method
- 基于 Cyclone IV 在 Quartus 中配置 IP 核中的 PLL、RAM 与 FIFO 的详细步骤及仿真验证
- ESP8266-Arduino编程实例-LED点阵驱动(基于Max7219)
- 密码学的基础:X.690和对应的BER CER DER编码
- 【leetcode热题Hot100】——任务调度器
- 【剑指offer】——股票的最大利润
- Jmeter TCP/UDP测试
- Sentinel vs Hystrix 限流对比,到底怎么选?
猜你喜欢

C语言入门--指针

用 SQL 做数据分析的十大常用功能,附面试原题解答!!

中原银行实时风控体系建设实践
软件测试个人求职简历该怎么写,模板在这里

leetcode:162. 寻找峰值

leetcode:152. 乘积最大子数组

浅谈用KUSTO查询语言(KQL)在Azure Synapse Analytics(Azure SQL DW)审计某DB账号的操作记录

【每日一题】622. 设计循环队列

高等代数_笔记_配方法标准化二次型

Best Practices for Migration from Jincang Database from MySQL to KingbaseES (3. MySQL Database Migration Practice)
随机推荐
用 SQL 做数据分析的十大常用功能,附面试原题解答!!
IDEA如何创建同级工程
高等代数_证明_矩阵乘以自身的转置的特征值不小于0
我的“眼睛”就是尺!
什么是数据标注? 数据标注公司主要做什么?
15【背景 渐变色】
记录学习--Navicat使用自定义数据库列表
【GraphQL】使用Hot Chocolate和.NET 6构建GraphQL应用
ClickHouse数据类型
Jincang Database OCCI Migration Guide (5. Program Development Example)
Auto.js Pro 计算脚本运行时间
ClickHouse卸载、重安装
stdio.h(本机代码)
Useful Monitoring Scripts What you want part1 in Oracle
C语言——结构体(声明、内存对齐、自引用)、位段、联合体、枚举常量合集
中非合作论坛非洲产品电商推广季启动 外交部:推动中非合作转型升级
MATLAB(5)绘图
软件测试个人求职简历该怎么写,模板在这里
Task Scheduler 计划定时任务,修改时报错: One or more of the specified arguments are not valid
银微转债,洁特转债上市价格预测