简体   繁体   中英

Calendar Class in Java

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.

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());

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)+" "
  );

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)

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM