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?

有帮助吗?

解决方案

MM means months. Use mm for minutes.

其他提示

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));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top