javacalendar

Java Calendar generates invalid month and date


Plase have a look at the below code

 Calendar date = Calendar.getInstance();
            initialClientLetterDate.setText(date.get(Calendar.YEAR)+"/"+date.get(Calendar.MONTH)+"/"+date.get(Calendar.DAY_OF_WEEK));

This generates the invalid "month" and "date". The output is 2014/09/06. Why is this? I just wanted to get current year, date and month.


Solution

  • You are using DAY_OF_WEEK which is 6 for FRIDAYand MONTH starts from 0 not 1 so you have to add 1 in it.You can use DAY_OF_MONTH instead of DAY_OF_WEEK.