如何在 Java15 中格式化字符串中的日期?

How can I format date in string in 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 及其格式 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>

如前所述,惯用的方法是使用 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 which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and