Question

I am trying to format date with Integer but they both work separately fine but when I put them together it throws following error.

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.util.Date
    at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at Test.main(Test.java:10)

Code:

public class Test {

    public static void main(String[] args) {
        String format = "http://az.website.com/api/calendar/date/%1$tY-%1$tm-%1$te/user_id/%d";

        Date date = Calendar.getInstance().getTime();
        System.out.println(String.format(format, date, 23));
    }

}

If I remove date format %1$tY-%1$tm-%1$te then it starts working fine but together it throws IlelgalFormatConversionExpception.

What's wrong with this code ?

Update

Code that runs but produces wrong out put.

public class Test {

    public static void main(String[] args) {
        String format = "http://az.website.com/api/calendar/date/%1$tY-%1$tm-%1$te/user_id/%2$%d";

        Date date = Calendar.getInstance().getTime();
        System.out.println(String.format(format, date) + 23);

    }

}

Output: http://az.website.com/api/calendar/date/2014-01-30/user_id/%d23

Code that produce erros:

public class Test {

    public static void main(String[] args) {
        String format = "http://az.website.com/api/calendar/date/%1$tY-%1$tm-%1$te/user_id/%2$d";

        Date date = Calendar.getInstance().getTime();
        System.out.println(String.format(format, date) + 23);

    }

}

Output:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '2$d'
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at Test.main(Test.java:10)

Using Java 1.6 Android 4.3 also tried on

java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

Update @Sotirios Delimanolis is really a life saver. I was calling the method wrongly. Wrong --> System.out.println(String.format(format, date) + 23);

Right --> System.out.println(String.format(format, date, 23));

Was it helpful?

Solution

Add a reference to the second argument in the argument list with 2$

String format = "http://az.website.com/api/calendar/date/%1$tY-%1$tm-%1$te/user_id/%2$d";

String.format(...) internally uses a Formatter whose javadoc states

The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top