0

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
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
juju
  • 553
  • 1
  • 4
  • 21
  • Possible duplicate of [Can't add days to Calendar](https://stackoverflow.com/questions/49539049/cant-add-days-to-calendar) – Ole V.V. Apr 08 '18 at 17:10
  • You may use [my answer here](https://stackoverflow.com/a/49717534/5772882). – Ole V.V. Apr 08 '18 at 17:15
  • Or as you see it possible duplicate of [Java: getMinutes and getHours](https://stackoverflow.com/questions/907170/java-getminutes-and-gethours) – Ole V.V. Apr 08 '18 at 17:16

5 Answers5

3

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).

lexicore
  • 42,748
  • 17
  • 132
  • 221
1

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

original provided code

me_
  • 681
  • 1
  • 8
  • 18
  • 1
    Has nothing to do with the reported problem. – lexicore Apr 08 '18 at 16:47
  • 1
    @lexicore it most certainly does... if he is trying to figure out why his minutes are different in two methods of accessing time, specifying minutes in his format is a prerequisite. – me_ Apr 08 '18 at 16:48
  • this was a last minute mistake before uploading, results were as stated in the question, just displaying incorrectly, updated now. – juju Apr 08 '18 at 16:52
  • i responded to the information you provided. The first thing that jumped out was a syntax error. – me_ Apr 08 '18 at 16:53
  • @me_ The OP prints the `Calendar.MINUTE` constant, so "unexpected" result has nothing to do with the date/time format, it is a much simpler error. Please check other answers which all point it out. – lexicore Apr 08 '18 at 16:55
  • @lexicore he was also printing out SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:ss");System.out.println("Date time = " + sdf.format(new Date())); and asking why the minutes could be different in each... https://stackoverflow.com/posts/49720176/revisions – me_ Apr 08 '18 at 17:00
  • @lexicore even assuming his usage of the calendar method was correct, 59 times every minute the print outs would have been different – me_ Apr 08 '18 at 17:02
  • @me_ OK, for that revision you're right the format was not correct. Removed the downvote. – lexicore Apr 08 '18 at 17:40
0

Calendar has a static constant field named MINUTE. You are printing that constant! That value has nothing to do with that date object.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

use Calendar.get(Calendar.MINUTE) instead

garritfra
  • 546
  • 4
  • 21
0

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)
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62