2

I am trying to print HOUR_OF_DAY,Minute and Second using Calendar Class. I used below command in my code.

  System.out.println(
      Calendar.HOUR+" "+
      Calendar.HOUR_OF_DAY+" "+
      Calendar.MINUTE+" "+
      Calendar.SECOND+" "
      );

It Gives output as below:

10 11 12 13 

Even when i ran this few hours back it gave same output.

I THOUGHT IT WILL PRINT CURRENT HOUR IN 24 HOUR FORMAT.But I am not getting that output.

So I want to know what this HOUR_OF_DAY, HOUR are supposed to print.

Please clarify.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35
  • Before asking how something works, always check the documentation. It can not get any clearer than _that_... – ppeterka Oct 03 '13 at 07:57
  • 1
    i checked but did not understoood – TheGraduateGuy Oct 03 '13 at 08:06
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/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/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 23 '18 at 05:24

6 Answers6

6

Calendar.HOUR, Calendar.HOUR_OF_DAY etc are just constants used to identify aspects of a date/time. They're typically used like this:

Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);

I can't easily think of any situation in which you'd want to print out the constants themselves.

Now even if you were using that, you'd still then be converting int values to String values via string concatenation - that has no idea about what format you want, because you're not specifying it explicitly. There's no such thing as a "2-digit-format int"... an int is just a number.

You should look into DateFormat and SimpleDateFormat - that's the preferred way to format dates and times using the standard Java class libraries. (I'd personally encourage you to look into Joda Time as a far better date/time API as well, but that's a different matter.)

For example:

DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
// Prints the current time using 24-hour format
System.out.println(formatter.format(new Date());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    We really need a "Beaten By Jon Skeet" badge on SO. You so frequently see other people complaining about being beaten by fractions of a minute to the same answer and think "get over it", but when it happens to you, you just feel ... I don't know, really amateur, I guess? FYI: I think you answered this one within about 40 seconds of it being posted. That's intense. – jr. Oct 03 '13 at 07:55
  • 2
    @studro in fact, he answered the question 40 seconds _before_ it was asked.... It just took so long to appear here... Or maybe out of sportsmanship, he didn't post the answer before the question appeared on the site. – ppeterka Oct 03 '13 at 07:58
  • @studro - haha +1, you won the comments – Eel Lee Oct 03 '13 at 08:34
  • 1
    @sturdo Jon's answers are already in SO database, including forthcoming ones, admins are only waiting for questions to be posted so that they provide the answers. ;) – Kira Oct 03 '13 at 10:11
  • Seems like Jon Skeet is RajniKant on SO. – TheGraduateGuy Oct 03 '13 at 10:25
3

try this

Calendar cal = Calendar.getInstance();

System.out.println(
  cal.get(Calendar.HOUR)+" "+
  cal.get(Calendar.HOUR_OF_DAY)+" "+
  cal.get(Calendar.MINUTE)+" "+
  cal.get(Calendar.SECOND)+" "
  );
Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
2

What you did is you printed values of Calendar Constants.

Create instance of Calendar using Calendar cal = Calendar.getInstance();(this will create instance of calendar with current time and date having default timezone and default locale)

You can print values of HOUR_OF_DAY, HOUR using cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.HOUR)

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

Take a look at GregorianCalender (http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html). This is probably what you're looking for.

user1021726
  • 638
  • 10
  • 23
0

Calendar.HOUR, Calendar.HOUR_OF_DAY, Calendar.MINUTE and Calendar.SECOND are constants used to access fields within a Calendar object. You need to create a Calendar object first, and then use these constants to access its fields using the get() method.

Try java.util.Calendar for more information.

I've been beaten by Jon Skeet - his answer is pretty much perfect and is a good starting point.

jr.
  • 1,699
  • 14
  • 31
0

tl;dr

( GregorianCalendar ) myCal   // Cast from interface `Calendar` to concrete class `GregorianCalendar`.
.toZonedDateTime()            // Convert from troublesome old legacy class to modern java.time class.
.getHour()                    // Extract the hour of the time-of-day portion.

java.time

The modern approach uses the java.time classes that supplanted the troublesome old Calendar class.

Assuming your Calendar is actually a GregorianCalendar, cast and convert. The old classes now have new methods to aid in converting to/from java.time.

ZonedDateTime is the java.time class replacing GregorianCalendar.

GregoriarCalendar gc = ( GregorianCalendar ) myCal ;
ZonedDateTime zdt = gc.toZonedDateTime() ;

Now interrogate for the parts you desire.

int hour   = zdt.getHour() ;
int minute = zdt.getMinute() ;
int second = zdt.getSecond() ;
int nano   = zdt.getNano() ;

Tip: You might want to extract a LocalTime object, to represent the time-of-day without date and without time zone.


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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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