Question

I am using a simple program to print the time:

DateFormat dateFormat1 = new SimpleDateFormat("MM");
Calendar cal1 = Calendar.getInstance();

String minute1=dateFormat1.format(cal1.getTime());


System.out.println(minute1);

However, this is only printing out "05" as the minute even when I test it several minutes later and it is not even close to "05" in the minutes in any time zone. What am I doing wrong?

Était-ce utile?

La solution

MM means months. Use mm for minutes.

Autres conseils

Read up on SimpleDateFormat. "MM" is months.

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

DateFormat dateFormat1 = new SimpleDateFormat("h:mm");

will give you the hour and minute in the form "9:45"

As previously mentioned "MM" is months.

But as you are already instantiating a calendar object, then why not simply do

System.out.println(cal1.get(Calendar.MINUTE));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top