java - 如何在 Java15 中格式化字符串中的日期?
问题描述
我在字符串文本中以定义的格式显示日期时遇到问题。
它会在应用程序运行后引发如下所示的错误。
Exception in thread "main" java.util.IllegalFormatConversionException: Y != java.lang.String
我该如何解决?
这是我的代码片段,如下所示。
String exampleFourText =
"""
<html>
<body>
<p> %s </p>
<p> %.1f </p>
<p> %d </p>
<p> %c </p>
<p> %1$tY-%1$tm-%1$td </p>
</body>
</html>
""";
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15 , 'y', new Date());
System.out.println(exampleFourText);
解决方案
您为论点使用了错误的序数。它应该%5
代替,%1
因为new Date()
是第 5 个参数。
import java.util.Date;
public class Main {
public static void main(String[] args) {
String exampleFourText = """
<html>
<body>
<p> %s </p>
<p> %.1f </p>
<p> %d </p>
<p> %c </p>
<p> %5$tY-%5$tm-%5$td </p>
</body>
</html>
""";
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15, 'y', new Date());
System.out.println(exampleFourText);
}
}
输出:
<html>
<body>
<p> Hello </p>
<p> 1234.6 </p>
<p> 15 </p>
<p> y </p>
<p> 2021-03-17 </p>
</body>
</html>
但是,惯用的方法是使用SimpleDateFormat
如下所示:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String exampleFourText = """
<html>
<body>
<p> %s </p>
<p> %.1f </p>
<p> %d </p>
<p> %c </p>
<p> %s </p>
</body>
</html>
""";
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15, 'y',
new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).format(new Date()));
System.out.println(exampleFourText);
}
}
输出:
<html>
<body>
<p> Hello </p>
<p> 1234.6 </p>
<p> 15 </p>
<p> y </p>
<p> 2021-03-17 </p>
</body>
</html>
请注意,java.util
日期时间 API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到现代日期时间 API *。
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
String exampleFourText = """
<html>
<body>
<p> %s </p>
<p> %.1f </p>
<p> %d </p>
<p> %c </p>
<p> %5$tY-%5$tm-%5$td </p>
</body>
</html>
""";
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15, 'y', LocalDate.now());
System.out.println(exampleFourText);
}
}
输出:
<html>
<body>
<p> Hello </p>
<p> 1234.6 </p>
<p> 15 </p>
<p> y </p>
<p> 2021-03-17 </p>
</body>
</html>
如前所述,惯用的方法是使用DateTimeFormatter
适用于现代日期时间 API 的日期时间格式化程序类型。但是,由于您想要的格式也是 的默认格式LocalDate#toString
,因此您不需要DateTimeFormatter
这种格式。只是为了完整起见,我还在DateTimeFormatter
下面的代码中展示了使用。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String exampleFourText = """
<html>
<body>
<p> %s </p>
<p> %.1f </p>
<p> %d </p>
<p> %c </p>
<p> %s </p>
</body>
</html>
""";
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15, 'y', LocalDate.now());
System.out.println(exampleFourText);
exampleFourText = exampleFourText.formatted("Hello", 1234.6, 15, 'y',
LocalDate.now().format(DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ENGLISH)));
System.out.println(exampleFourText);
}
}
输出:
<html>
<body>
<p> Hello </p>
<p> 1234.6 </p>
<p> 15 </p>
<p> y </p>
<p> 2021-03-17 </p>
</body>
</html>
<html>
<body>
<p> Hello </p>
<p> 1234.6 </p>
<p> 15 </p>
<p> y </p>
<p> 2021-03-17 </p>
</body>
</html>
从Trail: Date Time了解有关现代日期时间 API 的更多信息。
* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring和How to use ThreeTenABP in Android Project。
推荐阅读
- c# - Outlook 自动化
- java - java arraylist排序和优化
- javascript - 根据 x, y 位置计算角度
- typescript - 对象是“未知”类型的打字稿泛型
- ios - 将一个 iOS 应用程序集成到另一个 iOS 应用程序中
- python-3.x - Python Socket 下载上传数据
- c# - C#:接口和泛型:不能隐式转换类型
- angular - Angular 材质 mat-menu 的 Angular 性能问题
- java - Weblogic.application.ModuleException:java.lang.NoSuchFieldError:TRACE
- netty - netty中ChannelFuture和channelActive的顺序