Method toString
Method toString
(lines 34–39) takes no arguments and returns a String
in standard-time format, consisting of the hour
, minute
and second
values separated by colons and followed by AM or PM (e.g., 11:30:17 AM
or 1:27:06 PM
). Like method toUniversalString
, method toString
uses static String
method format
to format the minute
and second
as two-digit values, with leading zeros if necessary. Line 37 uses a conditional operator (?:
) to determine the value for hour
in the String
—if the hour
is 0
or 12
(AM or PM), it appears as 12; otherwise, it appears as a value from 1 to 11. The conditional operator in line 30 determines whether AM or PM will be returned as part of the String
.
Recall all objects in Java have a toString
method that ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.