java - 添加日期格式并以时间格式提供 am/pm
问题描述
日期 API 中是否有任何特定格式可以在特定日期之后打印 st、nd、rd 和 th。说 12 月 26 日应该是 12 月 26 日。
还有任何特定的时间格式,比如上午 10.30 格式应该完全相同。
只需要内置模式来解决这个问题。
我已经为场景编写了下面的代码,但没有得到任何想要的输出
//Checking date format
String pattern = "EEE, MMM.dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
//checking time format
DateFormat dateFormat2 = new SimpleDateFormat("hh.mm aa.");
String dateString2 = dateFormat2.format(new Date()).toString();
System.out.println(dateString2);
解决方案
不,没有这种格式。正如您在文档中看到的那样,日期和时间模式SimpleDateFormat
不提供诸如26th
.
推荐阅读
- c# - 将 WPF 数据网格导出到 PDF 文件
- java - 无法从其他片段加载片段
- javascript - PHP 和 Javascript 问题。函数总是返回最后一个值
- bitcoinj - Bitcoinj get transactions from blockchain
- powershell - PowerShell 读取 csv 文件并相应地创建文件
- c++ - 如何重构重复的控制结构?
- r - 如何从知道相机像素分辨率、焦距和高度的照片开始计算 R 中的叶面积?
- python - 无法解析树莓派上的主机地址
- javascript - [Vue 警告]:渲染错误:“TypeError:无法读取属性 'id' of null”
- c# - 在 c# 和 c++ 之间将 double 类型的二维多维数组作为输入和输出的 pinvoke 编组