I can’t be sure from the information you have provided, but the likely cause is that your JVM’s time zone setting is UTC. If you ran your program at, say, 15:09:55 Singapore time, that would print as 7 and 9 in UTC. If you then read your clock in Singapore time a little over a minute later, or read it from a clock that wasn’t in perfect synch with your device (or simulator), it would show 3:11 PM.
The JVM usually picks up its time zone setting from the device, but there can be all sorts of reasons why it is getting it from somewhere else.
As Anton A. said in a comment, you need HOUR_OF_DAY
instead of HOUR
for 24 hour format (though the time was 7:09 AM in UTC, so this error wasn’t the cause of the unexpected hour in your case).
java.time
The modern way to print the current time is:
System.out.println(LocalTime.now(ZoneId.of("Asia/Singapore")));
The Calendar
class that you were using is long outdated and poorly designed. LocalTime
is the class from java.time, the modern Java date and time API, that represents the time of day (without time zone, but the now
method accepts a time zone to initialize to the current time in that zone). If your Android device is less than new (under API level 26), you will need the ThreeTenABP library in order to use it. See How to use ThreeTenABP in Android Project.
One more link: Oracle tutorial: Date Time explaining how to use java.time
.