0

I am trying to set the calendar hour and minute by using the following code

Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR,4);
c.set(Calendar.MINUTE, 23);

but it is always displaying 11 as hour and 12 as minute.

  • What do you mean, it displays? When you hover your mouse in `Calendar.HOUR` and `Calendar.MINUTE`? – joao2fast4u Apr 18 '15 at 17:18
  • Where are you displaying it in your code? – keshav kowshik Apr 18 '15 at 17:41
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 01 '18 at 06:36

1 Answers1

0

tl;dr

ZonedDateTime.of(                                        // Represent a moment on the timeline adjusted into a particular time zone.
    LocalDate.now( ZoneId.of( "Pacific/Auckland" )  ) ,  // Capture the current date for a particular time zone.
    LocalTime.of( 4 , 23 ) ,                             // Hard-code the time-of-day value desired. Will be adjusted as needed to handle anomalies such as DST.
    ZoneId.of( "Pacific/Auckland" )                      // Apply a particular time zone to this date-time.
)

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

You seem to want the current date with a specific time-of-day.

You ignore the crucial issue of time zone.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

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

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Determine today’s current date for a particular time zone.

LocalDate today = LocalDate.now( z ) ; 

Represent your target time-of-day.

LocalTime lt = LocalTime.of( 4 , 23 ) ;

Combine with zone to get a ZonedDateTime. If your time-of-day is not valid for that date in that zone because of anomalies such as Daylight Saving Time (DST), the class automatically adjusts the time.

ZonedDateTime zdt = ZonedDateTime.of( today , lt , z ) ;

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.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154