While using Calendar class of java I am getting an error, the term -Xdiags:verbos is there in error message
Code:
public class CalendarClass{
public static void main(String args[]){
Calendar cal=Calendar.getInstance();
System.out.println(cal.get(Calendar.DATE " / " Calendar.MONTH " / " Calendar.YEAR ));
}
}
Output:
CalendarClass.java:5: error: incompatible types: String cannot be converted to int System.out.println(cal.get(Calendar.DATE " / " Calendar.MONTH " / " Calendar.YEAR )); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
So how I should resolve this?
CodePudding user response:
You can't create a new syntax for Calendar.get(int) (it takes an int, not a String). Change
System.out.println(cal.get(Calendar.DATE " / "
Calendar.MONTH " / " Calendar.YEAR ));
to
System.out.println(cal.get(Calendar.DAY_OF_MONTH)
" / " (cal.get(Calendar.MONTH) 1) " / "
cal.get(Calendar.YEAR));
But I would strongly suggest you replace all of that with the more modern java.time equivalent. Like,
System.out.println(DateTimeFormatter.ofPattern("dd / M / yyyy")
.format(LocalDate.now()));
Both of which output (because today is January 30, 2022)
30 / 1 / 2022
CodePudding user response:
The main issue is answered already. Calendar.get needs an int as parameter but your code delivers a String from the string concatenation Calendar.DATE " / " Calendar.MONTH " / " Calendar.YEAR.
To answer your question about the note
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
It tells you that you could get a more verbose diagnostic mode while compiling when you add extra option -Xdiags:verbose to javac. In concrete example:
Without -Xdiags:verbose:
CalendarClass.java:N: error: incompatible types: String cannot be converted to int
System.out.println(cal.get(Calendar.DATE " / " Calendar.MONTH " / " Calendar.YEAR ));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
The message points to the last in string concatenation.
With -Xdiags:verbose:
CalendarClass.java:N: error: method get in class Calendar cannot be applied to given types;
System.out.println(cal.get(Calendar.DATE " / " Calendar.MONTH " / " Calendar.YEAR ));
^
required: int
found: String
reason: argument mismatch; String cannot be converted to int
1 error
The message points to the method cal.get.
For me the latter is more useful. That's why I always have that extra option set.
How to set compiler options?
That depends on how you are compiling. Using javac directly it is as simple as putting the compiler options in javac command line:
javac -encoding UTF-8 -Xlint:deprecation -Xdiags:verbose -cp .;./depJars/* CalendarClass.java
Using an IDE please look at IDE's documentation on how to set extra options to javac.
