Randomly Named User
Randomly Named User

Reputation: 1949

Only default values are returned in Calendar object in android. Why?

I have set a date to a calendar object in this way...

Calendar lastCheckUp = Calendar.getInstance();
lastCheckUp.set(year, month+1, day);

Now, when I print this out in the console using

System.out.println(lastCheckUp);

I get the correct values...

07-18 11:59:13.903: I/System.out(1717): java.util.GregorianCalendar[time=1365834504001,areFieldsSet=true,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=2,DAY_OF_MONTH=13,DAY_OF_YEAR=103,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=58,SECOND=24,MILLISECOND=1,ZONE_OFFSET=19800000,DST_OFFSET=0]

So i'm assuming that all the values are set correctly in the calendar object.

But when I try to access it using

mTextViewLastCheckDate.setText(new StringBuilder().append(lastCheckUp.DAY_OF_MONTH)
            .append("/").append(lastCheckUp.MONTH).append("/").append(lastCheckUp.YEAR)
            .append(" "));

I get only the default values...

That is, my textview gives an output of 5/2/1

What am i doing wrong?

Upvotes: 0

Views: 1033

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500385

You're using lastCheckup.MONTH, lastCheckup.DAY_OF_MONTH etc. Those are constant fields - to access the values for a specific calendar, you need

int month = lastCheckUp.get(Calendar.MONTH);

etc. Read the documentation of Calendar for more details about how you're meant to use it.

However, you would also need to understand that months are 0-based in Calendar, so it still wouldn't look right. Also, you almost certainly want to 0-pad the day and month. You'd be much better off using SimpleDateFormat to do this for you.

// Not sure why you want a space at the end, but...
DateFormat format = new SimpleDateFormat("dd/MM/yyyy ");
mTextViewLastCheckDate.setText(format.format(lastCheckup.getTime());

You should consider which time zone and locale you want to use, too. The above code just uses the default.

EDIT: Note that this line:

lastCheckUp.set(year, month+1, day);

is almost certainly wrong. We don't know what month is really meant to be here, but in the set call it should be in the range 0-11 inclusive (assuming a Gregorian calendar).

Upvotes: 3

Mena
Mena

Reputation: 48404

Your problem is how you are accessing the Calendar's get method and your reference to Calendar constants.

Try this:

mTextViewLastCheckDate.setText(
  new StringBuilder().append(lastCheckUp.get(Calendar.DAY_OF_MONTH))              
  .append("/")
  .append(lastCheckUp.get(Calendar.MONTH)) // beware, Calendar months are 0 based
  // please refer to Jon Skeet's solution for a better print out of your date
  // or add +1 to the month value here, instead of setting your Calendar with month + 1. 
  // Otherwise December will not work. See also comments below. 
  .append("/")
  .append(lastCheckUp.get(Calendar.YEAR))
  .append(" ")
);

By the way, most IDEs will warn you when you are trying to access a class' constants from an instance of said class. This can help you figure out what you're doing wrong usually.

Upvotes: 1

Related Questions