Reputation: 555
The code below shows the text of a parsed Date
object, and the static field Calendar.MINUTE
. Can someone inform why they're different?
The docs say it's supposed to get the current minute value as an int.
EDIT: image removed, code/result updated.
public class Testerson {
public void print()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
System.out.println("Date time = " + sdf.format(new Date()));
System.out.println("Calendar Minute = " + Calendar.MINUTE);
}
public static void main(String[] args) throws ParseException
{
Testerson test = new Testerson();
test.print();
}
}
With an output of;
Date time = 08/04/2018 05:49
Calendar Minute = 12
Upvotes: 0
Views: 1241
Reputation: 743
you didn't specify minutes you specified seconds in your date format.
your date time is "dd/MM/yyyy hh:ss" it should be "dd/MM/yyyy HH:mm"
original question: https://stackoverflow.com/posts/49720176/revisions
Upvotes: 1
Reputation: 11686
Calendar.MINUTE
is a constant (static final
). Since, it’s a constant there is no chance it can give you the current minute, as it should change its value every moment. Thus, your understanding is wrong.
The docs says it’s a field number to get and set the Minute of hour. You need to use it to extract the minute value from the calendar instance like below:
calendar.get(Calendar.MINUTE)
Upvotes: 0
Reputation: 43671
Calendar.MINUTE
is simply a constant:
public final static int MINUTE = 12;
If you have an instance of Calendar
you could get the minute as calendar.get(Calendar.MINUTE)
.
Upvotes: 3
Reputation: 140465
Calendar has a static constant field named MINUTE. You are printing that constant! That value has nothing to do with that date object.
Upvotes: 0