-1
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
String data = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + " " + calendar.get(Calendar.DAY_OF_MONTH) + "-"
        + calendar.get(Calendar.MONTH) + "-"
        + calendar.get(Calendar.YEAR);
System.out.println(data);

nad print: 23:3 15-6-2015 but today is 23:03 15-7-2015 Month is previous and how to change 23:3 to 23:03 ?

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
dekros
  • 59
  • 1
  • 8

1 Answers1

1

It's completely non-intuitive and annoying, but that's how the API was designed; the value returned when you ask for a month is zero-based; you have to add one to it to get the "real" month-number.

Check out the documentation for the Calendar.MONTH field:

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year. (emphasis mine)

Also, instead of building the string out by hand it would be far easier to use something like SimpleDateFormat; you won't run into this issue then, and you will also be able to print out the time as 23:03:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd-MM-yyyy");
sdf.setTimeZone(calendar.getTimeZone());
System.out.println(dateFormat.format(calendar.getTime()));
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295