-1

I am trying to get the current time in 24 hours format by using the code below:

Calendar cal = Calendar.getInstance();
int currentHour = cal.get(Calendar.HOUR);
int currentMinute = cal.get(Calendar.MINUTE);

System.out.println(currentHour);
System.out.println(currentMinute);

My current time is 3.11PM Singapore time. But then when I execute the code above, I am getting 7.09. Any ideas?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
dummygg
  • 233
  • 1
  • 3
  • 12
  • Use HOUR_OF_DAY instread of HOUR – Anton A. Aug 30 '18 at 07:15
  • @AntonA. that still doesnt match the output given that at least the minutes are actually matching – XtremeBaumer Aug 30 '18 at 07:16
  • @XtremeBaumer are you sure you phone time does not equal output? – Anton A. Aug 30 '18 at 07:18
  • @AntonA. I am not the OP. But looking at the given hours, his code should actually output `3:11` and not `7:09` or `7:11` – XtremeBaumer Aug 30 '18 at 07:19
  • Check the default Locale, or do `getInstance(new Locale("CN"))` or what fits for Singapore. – Joop Eggen Aug 30 '18 at 07:20
  • Possible duplicate of [Getting the current time and date on a 24 hour timescale](https://stackoverflow.com/questions/7280745/getting-the-current-time-and-date-on-a-24-hour-timescale) – ADM Aug 30 '18 at 07:24
  • 1
    @ADM It is not duplicate. I tried that as well but the hour is wrong, just that the minute is correct – dummygg Aug 30 '18 at 07:25
  • @dummygg, you need to check the default timezone value, TimeZone.getDefault(). – Ankur Aug 30 '18 at 07:26
  • @Ankur I tried with this: Calendar.getInstance(TimeZone.getTimeZone("GMT")); But I am still getting the wrong hour :( – dummygg Aug 30 '18 at 07:31
  • Its clearly a duplicate . You need to check few things `TimeZone` and `DeviceTime`. You are getting time as per UK TimeZONE See the difference in GMT . Cause Singapore is on ` (GMT+8) ` and UK in on ` (GMT-0) ` – ADM Aug 30 '18 at 07:37
  • Locale isn’t relevant. Time zone is. The two are unrelated. – Ole V.V. Aug 30 '18 at 07:43
  • I cannot reproduce either. I get 9 and 43, which agrees with my computer clock. – Ole V.V. Aug 30 '18 at 07:44
  • @dummygg, you would get different value as GMT is not the timezone for singapore, i guess. I asked you to check the default timezone. But you can also try "Asia/Singapore" in place of "GMT" to get your time according to Singapore – Ankur Aug 30 '18 at 09:01

3 Answers3

3

I figured the solution already:

Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
currentHour = cal.get(Calendar.HOUR_OF_DAY);
currentMinute = cal.get(Calendar.MINUTE);

The code above returns exactly what I wanted

dummygg
  • 233
  • 1
  • 3
  • 12
1

This will give you the time in 24 hr format

public String getFormattedTime() {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
    return dateFormatter.format(calendar.getTime());
}
  • But the hour returned was wrong, the minute was correct though – dummygg Aug 30 '18 at 07:27
  • Try this method. Hope it works "String formattedDate = dateFormat.format(new Date()).toString();" – Muhammed Arshad K Aug 30 '18 at 07:39
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option or without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). Also your code won’t solve the asker’s issue. – Ole V.V. Aug 30 '18 at 11:59
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161