Reputation: 1520
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.
Upvotes: 2
Views: 6288
Reputation: 338276
( 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.
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.
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.
Upvotes: 0
Reputation: 16158
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)
Upvotes: 2
Reputation: 1739
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.
Upvotes: 0
Reputation: 1853
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)+" "
);
Upvotes: 3
Reputation: 648
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.
Upvotes: 0
Reputation: 1499880
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());
Upvotes: 6