Tristan Wiesepape
Tristan Wiesepape

Reputation: 17

Android - Calendar.getInstance has the hours value set to zero

I'm trying to find the current time in my java code.

Calendar cal = Calendar.getInstance();
    Date currentLocalTime = cal.getTime();
    DateFormat date = new SimpleDateFormat("HH:mm a");
    String localTime = date.format(currentLocalTime);
    ((TextView) (mainView.findViewById(R.id.l1))).setText(localTime);

For some reason the textview displays 01:08 AM instead of 8:08 PM.

Upvotes: 0

Views: 841

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340230

tl;dr

As stated in correct Answer by Don Brody, your formatting pattern was incorrect, using HH (for 24-hour clock) where it should have been lowercase hh (for 12-hour clock). You likely also have a problem with your JVM’s current default time zone not being set to what you expect.

Also… Your problem is moot. You are using terrible old classes that were supplanted years ago by java.time.

LocalTime                                  // Represent time-of-day without date and without time zone.
.now()                                     // Capture the current time-of-day as seen in the JVM’s current default time zone. Better to pass the optional `ZoneId` argument to specify explicitly the desired/expected time zone.
.format(                                   // Generate a `String` representing our time-of-day value.
    DateTimeFormatter
    .ofLocalizedTime( FormatStyle.SHORT )  // Automatically localize rather than hard-code a specific formatting pattern.
    .withLocale( Locale.US )               // Locale determines the human language and cultural norms used in localizing.
)                                          // Returns a `String` object.

10:09 PM

Use java.time

You are using terrible old classes now supplanted by the java.time classes.

Get the current time-of-day.

LocalTime lt = LocalTime.now() ;  // Capture the current time-of-day using the JVM’s current default time zone.

Better to explicitly state the desired/expected time zone than rely implicitly on the JVM’s current default time zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalTime lt = LocalTime.now( z ) ;

Generate a string in AM-PM format.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "hh:mm a" ) ;  // Lowercase `hh` for 12-hour clock, uppercase `HH` for 24-hour clock.
String output = lt.format( f ) ;

Even better, let java.time automatically localize for you.

Locale locale = Locale.US ;
DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale ) ;
String output2 = lt.format( f2 ) ;

See that code run live at IdeOne.com.

lt.toString(): 22:09:19.825

output: 10:09 PM

output2: 10:09 PM


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Upvotes: 2

Don Brody
Don Brody

Reputation: 1727

If you want the 12 hour time (8:08 PM instead of 20:08 PM) make sure to change your HH to hh like so:

DateFormat date = new SimpleDateFormat("hh:mm a");

That doesn't explain why you're seeing 01:08 AM, but it's a start. I tried the exact same code out on my system, and the time displayed properly. So, I'm not sure where your issue is. My guess is that you either have your system clock set incorrectly, or more likely your experiencing a weird issue with the Calendar and Date libraries. If you continue to experience problems I would recommend switching to the Joda-Time library. It's awesome, and it fixes all of the little issues you'll run into while using the Date and Calendar libraries.

Upvotes: 2

Alifyz Pires
Alifyz Pires

Reputation: 73

You can use the following code:

Date currentDate = Calendar.getInstance(TimeZone.getDefault()).getTime()

Check out this other question that might be an answer to your problem. How to get the current date and time of your timezone in Java?

Upvotes: 0

Related Questions