[Solved] java.util.UnknownFormatConversionException: Conversion = ‘y’

java.util.UnknownFormatConversionException  is unchecked runtime exception which is sub class of java.util.IllegalFormatException. This exception occurs while using unknown parameter values while formatting data.

Constructors

  • UnknownFormatConversionException(String message) : Constructs a instance of this class with unknown conversion  message.

Sample Code

here in below example for extract values of date, day, month, year, hour and minute values from LocalDateTime and printing in particular format bySystem.out.printf()

System.out.println("*********Get Date and Time Values***************");
LocalDateTime dateTimePoint=LocalDateTime.now();
LocalDate datePoint = dateTimePoint.toLocalDate();
Month month = dateTimePoint.getMonth();
int day = dateTimePoint.getDayOfMonth();
int year = dateTimePoint.getYear();
int hour = dateTimePoint.getHour();
int minute = dateTimePoint.getMinute();
int second = dateTimePoint.getSecond();
System.out.println("Local Date :" + datePoint);
System.out.printf("Local Date time Format : %d/%d/%d %d:%d:%y", day, month.getValue(), year, hour, minute,second);

Output Message


Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'y'
    at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)

Issue

Here throw UnknowFormatConversionException for parameter value as y for printing values of second. it is because of just typing mistake. But in below solution mentioned on what cases it can occurd and how to resolve it.

Solutions

To solve this issue replace System.out.printf value y as d because for representing numeric value param value is d.

Follow below links to know about supported conversion format and parameters

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax

More Issues Solution

For more other JAVA/JDBC issues solution follow link JAVA/JDBC Issues.