【问题标题】:How can I format date in string in Java15?如何在 Java 15 中格式化字符串中的日期?
【发布时间】:2021-06-14 20:08:43
【问题描述】:

我在字符串文本中以定义的格式显示日期时遇到问题。

应用运行后会抛出如下所示的错误。

Exception in thread "main" java.util.IllegalFormatConversionException: Y != java.lang.String

我该如何解决?

这是我的代码 sn-p,如下所示。

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);

【问题讨论】:

  • formatted 来自哪里?
  • @fantaghirocco 这是Java15的新特性。
  • 谢谢,不知道
  • @Pshemo 15 已在 %d 中显示
  • 我建议你不要使用Date。该课程设计不良且早已过时。而是使用来自java.time, the modern Java date and time APILocalDate。我还没有检查,但我敢打赌它也适用于 formatted(),所以你只需要将 new Date() 更改为 LocalDate.now(ZoneId.systemDefault())

标签: java string date format java-15


【解决方案1】:

你为参数使用了错误的序数。它应该是%5 而不是%1,因为new Date() 是第五个参数。

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 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern date-time 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>

如前所述,惯用的方法是使用日期时间格式化程序类型,即现代日期时间 API 的 DateTimeFormatter。但是,由于您所需的格式也是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 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 2014-09-05
    • 1970-01-01
    • 2022-10-13
    • 2013-03-28
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多