user20191130
user20191130

Reputation: 360

GregorianCalendar in Android (add Calendar.HOUR_OF_DAY vs Calendar.HOUR)

To add an hour to current time, can I use this?

Calendar mcalendar = new GregorianCalendar();
mcalendar.add(Calendar.HOUR_OF_DAY, 1); //I plan to use 24 hours format

I see many examples using instead:

Calendar mcalendar = Calendar.getInstance();
mcalendar.add(Calendar.HOUR, 1);

Upvotes: 0

Views: 405

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339679

tl;dr

Duration duration = Duration.ofHours( 1 ) ;
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( zoneId ).plus( duration ) ;
DateTimeFormatter f = DateTimeFormatter
                      .ofLocalizedDateTime( FormatStyle.FULL )
                      .withLocale( Locale.GERMANY) ;
String output = zdt.format( f ) ;

Sonntag, 27. Dezember 2020 um 22:58:52 Nordamerikanische Ostküsten-Normalzeit

java.time

The modern solution uses the java.time classes. Never use Calendar or GregorianCalendar.

UTC

Capture the current moment as seen in UTC. Use Instant.

Instant instant = Instant.now() ;  // Capture the current moment as seen in UTC.

Define a span-of-time unattached to the timeline.

Duration duration = Duration.ofHours( 1 ) ;

Addition.

Instant instantHourLater = instant.plus( duration ) ;

Zoned

You may want to see the time-of-day and date of that moment as seen through the wall-clock time used by the people of a particular region. Apply a time zone (ZoneId) to get a ZonedDateTime object. Same moment, same point on the timeline, different wall-clock time.

ZoneId zoneId = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( zoneId ) ;

Or, skip the Instant and the Duration.

ZonedDateTime zdt = ZonedDateTime.now( zoneId ).plusHours( 1 ) ;

Text

You said:

I plan to use 24 hours format

The classes such as Instant, OffsetDateTime, and ZonedDateTime represent a moment, a point on the timeline. They have nothing to do with text. They do not have a “format”. They can parse text representing a moment, and they can produce text representing a moment. But internally they have their own way of representing that moment, without any formatted text.

To produce text in a particular format, use DateTimeFormatter class with FormatStyle and Locale. This has been covered many many times already on Stack Overflow. So search to learn more.

DateTimeFormatter f = DateTimeFormatter
                      .ofLocalizedDateTime( FormatStyle.FULL )
                      .withLocale( Locale.CANADA_FRENCH ) 
;
String output = zdt.format( f ) ;

See this code run live at IdeOne.com.

output: dimanche 27 décembre 2020 à 22 h 51 min 57 s heure normale de l’Est


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.

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

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

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. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

Upvotes: 2

Lakindu Hewawasam
Lakindu Hewawasam

Reputation: 750

Calendar mcalendar = new GregorianCalendar();

The GreogorianCalendar is a subclass of the abstract class Calendar. Therefore what you are doing here is referencing an instance of the GregorianCalendar to the Calendar therefore all the abstract methods in Calendar will follow the implementation of GregorianCalendar

But since your purpose is to add Hours.

You can go ahead with Calendar mcalendar = Calendar.getInstance(); as it retrieves an instance to the current with the the current Locale.

However, if you want to change your Locale, pass in the parameter into the getInstance() method and an instance of the specified Locale will be generated for you.

Refer: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getInstance(java.util.Locale)

And if you want to use Hours:

Do the following:

Do not use HOUR variable as this is only for 12 hour times. Use Calendar.HOUR_Of_DAY to deal with 24 hour timings.

Upvotes: 0

Related Questions