PrintStream类型中的printf(String,Object [])方法不适用于参数(String,String,int)

时间:2017-03-22 21:31:12

标签: java string

我正在输入此代码:

import java.util.Calendar;  //Required to get the instance of date on the computer
import java.util.Date;      //Required if you want to store instances of the calendar in a varaible
import java.util.Scanner;   //Required to collect input from the user


public class L01ManagingDate 
{

    public static void main(String[] args)
    {


    Calendar cal = Calendar.getInstance();  

    System.out.println(cal);    

    System.out.println("The current date list printed out, but not stored " + cal.getTime());

    Date dateNow = cal.getTime();   
    System.out.println("The current date list stored in a variable " + dateNow);

    System.out.printf("%-22s %d%n" , "Year" , cal.get(Calendar.YEAR));
    System.out.printf("%-22s %d%n" , "Month" , cal.get(Calendar.MONTH));
    System.out.printf("%-22s %d%n" , "Month" , cal.get(Calendar.DAY_OF_MONTH));
    System.out.printf("%-22s %d%n" , "Week" , cal.get(Calendar.DAY_OF_WEEK));
    System.out.printf("%-22s %d%n" , "Day" , cal.get(Calendar.DAY_OF_YEAR));
    System.out.printf("%-22s %d%n" , "Week" , cal.get(Calendar.WEEK_OF_YEAR));
    System.out.printf("%-22s %d%n" , "Month" , cal.get(Calendar.WEEK_OF_MONTH));
    System.out.printf("%-22s %d%n" , "Day of week in month" , cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.printf("%-22s %d%n" , "AM (0) or PM (1)" , cal.get(Calendar.AM_PM));
    System.out.printf("%-22s %d%n" , "Hour" , cal.get(Calendar.HOUR_OF_DAY));
    System.out.printf("%-22s %d%n" , "Minute" , cal.get(Calendar.MINUTE));
    System.out.printf("%-22s %d%n" , "Second" , cal.get(Calendar.SECOND));
    System.out.printf("%-22s %d%n" , "Millisecond" , cal.get(Calendar.MILLISECOND));

我在所有打印F上都出现此错误PrintStream类型中的printf(String,Object [])方法不适用于参数(String,String,int) 请帮忙

1 个答案:

答案 0 :(得分:-1)

将每个语句更改为如下所示。不要将第二个参数作为字符串see here

传递
 System.out.printf("%-22s %d%n Year" , cal.get(Calendar.YEAR));