当前位置:网站首页>阿里面试官:聊聊如何格式化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库来完成同样的事情。
边栏推荐
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(二)基于arduino的简易示波器
- 【剑指offer】——股票的最大利润
- 一文了解SAP IBP是什么?
- Jincang Database Pro*C Migration Guide ( 5. Program Development Example)
- 2022-08-02 顾宇佳 学习笔记 多线程
- leetcode:149. 直线上最多的点数
- Go新项目-编译项目的细节(4)
- 找不到符号@SuperBuilder,你以为真的是Lombok的问题?
- vscode access denied to unins000.exe
- Domino服务器SSL证书安装指南
猜你喜欢
C语言——-动态内存开辟与管理(malloc,free,calloc,realloc)+柔性数组
【TA-霜狼_may-《百人计划》】美术2.5 模型常见问题及规范
机器学习【KNN案例、API、总结】
【leetcode热题Hot100】——任务调度器
一文了解SAP IBP是什么?
基于 Cyclone IV 在 Quartus 中配置 IP 核中的 PLL、RAM 与 FIFO 的详细步骤及仿真验证
C语言——结构体(声明、内存对齐、自引用)、位段、联合体、枚举常量合集
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(3. MySQL 数据库移植实战)
【GraphQL】使用Hot Chocolate和.NET 6构建GraphQL应用
iScroll系列之下拉刷新 + 上拉加载更多
随机推荐
工业边缘计算研究现状与展望
uniapp中动态修改导航栏标题
IDEA如何创建同级工程
log4j设置日志的时区
使用docker容器搭建MySQL主从复制
数字3d虚拟交互展厅顺应时代发展需求和趋势
Chapter 8 Character Input Output and Input Validation
PyTorch安装——安装PyTorch前在conda搭建虚拟环境的报错
积分商城可设置的四种兑换商品类型
【动态规划--01背包】HJ16 购物单
详细讲解一下JVM的内存模型与实现?
多线程使用哈希表
【leetcode热题Hot100】——任务调度器
Jmeter TCP/UDP测试
Compose the displacement of the view
Jincang Database Pro*C Migration Guide (3. KingbaseES Pr*oc Compatibility with Oracle Pro*c)
ClickHouse delete table
Guys, I don't understand a bit: why the documentation of oracle-cdc writes that the connector can be done exactly-o
C语言实验十三 指针(三)
基于flowable的upp(统一流程平台)运行性能优化(3)