Reputation: 2456
When I compile this code by compiler compliance level 6.0 in eclipse then it compile fine but when I change compiler level 4.0 then this code shows error that Incompatible Conditional operand types String and Integer. What is the problem and changes this code need to get the same result
Calendar cal = Calendar.getInstance();
String timeStr = (calendar.get(Calendar.HOUR_OF_DAY) < 10 ?
"0" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) :
Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY))) + ":" +
(calendar.get(Calendar.MINUTE) < 10 ?
"0" + Integer.valueOf(calendar.get(Calendar.MINUTE)) :
Integer.valueOf(calendar.get(Calendar.MINUTE)));
Upvotes: 0
Views: 100
Reputation: 719310
The real problem is those Integer.valueOf(...)
calls that are turning int
values into Integer
objects. This is not doing anything useful. When the compiler level is 4.0 you get a compilation error because Java 1.4.x doesn't support unboxing, and the operands of the ternary operator are therefore incompatible.
If we strip out the Integer.valueOf
stuff it looks like this:
String timeStr =
(calendar.get(Calendar.HOUR_OF_DAY) < 10 ?
"0" + calendar.get(Calendar.HOUR_OF_DAY) :
calendar.get(Calendar.HOUR_OF_DAY)) +
":" +
(calendar.get(Calendar.MINUTE) < 10 ?
"0" + calendar.get(Calendar.MINUTE) :
calendar.get(Calendar.MINUTE));
That still won't compile because the types of the 2nd and 3rd operands of the ternary operator don't match. That's easy to fix:
String timeStr =
(calendar.get(Calendar.HOUR_OF_DAY) < 10 ?
"0" + calendar.get(Calendar.HOUR_OF_DAY) :
"" + calendar.get(Calendar.HOUR_OF_DAY)) +
":" +
(calendar.get(Calendar.MINUTE) < 10 ?
"0" + calendar.get(Calendar.MINUTE) :
"" + calendar.get(Calendar.MINUTE));
(The compiler will probably optimize away the concatenation with ""
...)
Finally, you can refactor it further to give this:
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
String timeStr =
(hours < 10 ? "0" : "") + hours + ":" +
(minutes < 10 ? "0" : "") + minutes;
Upvotes: 1
Reputation: 72049
Why not just use SimpleDateFormat
to get the same result, like this:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String timeStr = sdf.format(cal.getTime());
It works with the compiler compliance level set to 1.4.
Edit: for reference if SimpleDateFormat
didn't exist, I'd recommend writing it something like this:
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
String timeStr = (hour < 10 ? "0" : "") + hour + ":"
+ (minute < 10 ? "0" : "") + minute;
Upvotes: 1
Reputation: 114817
Quick fix: add some empty Strings (if you want to keep your code):
String timeStr =
(calendar.get(Calendar.HOUR_OF_DAY) < 10 ?
"0" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) :
"" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)))
+ ":" +
(calendar.get(Calendar.MINUTE) < 10 ?
"0" + Integer.valueOf(calendar.get(Calendar.MINUTE)) :
"" + Integer.valueOf(calendar.get(Calendar.MINUTE)));
(But I recommend the simple formatter too - you have a lot of avoidable hidden conversions from String to Integer to int and back to String)
Upvotes: 0
Reputation: 13799
Most human programmers are likely to be confused by your code; I am guessing that the compiler is too. :-)
Somewhat better style, but not directly answering your question would be:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String timeStr = sdf.format(cal);
Upvotes: 0
Reputation: 48596
The problem is that if HOUR_OF_DAY is < 10, the conditional operator will evaluate to a String, whereas if not, it will evaluate to an Integer. Under 4.0, that was considered an error (as the compiler helpfully pointed out.)
To fix it, you should probably just use one of the built in date-formatters, rather than having complex logic that doesn't always compile. For example:
SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm");
String theCurrentTime = dateFormatter.format(cal.getTime());
If you really needed to keep the logic, though, you could fix it by putting "" +
in front of the Integer.valueOf()
call in the :
branch of the conditional operator. Not the best option, though.
Upvotes: 2