Alan Coromano
Alan Coromano

Reputation: 26048

getYear(), getMonth(), getDay are deprecated in Calendar, what to use then?

I want to parse a date from a string and set it in the DatePickerDialog:

try {
    myCalendar.setTime(mySimpleFormatter.parse(jsonObj.getString("dob")));
} catch (ParseException e) {
    System.out.println("!!!");
}

myEditBox.setText(mySimpleFormatter.format(myCalendar.getTime()));
myDatePickerDialog.getDatePicker().updateDate(myCalendar.getTime().getYear()); // depricated

But the issue is that myCalendar.getTime().getYear(), getMonth(), getDay are deprecated. What should be used then?

Upvotes: 11

Views: 21442

Answers (5)

Oussema Helal
Oussema Helal

Reputation: 9

Kotlin solution :

Calendar.getInstance().get(Calendar.MONTH + 1)

The value returned from get(Calendar.MONTH) is between 0 and 11, with the value 0 representing January, so you need to add +1 to get the current month.

Upvotes: 0

Pablo C. García
Pablo C. García

Reputation: 22404

Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
int day= cal.get(Calendar.DAY_OF_MONTH);

Upvotes: 19

Yasir Tahir
Yasir Tahir

Reputation: 800

I will recommend you to use JodaTime instead as it is more powerful and can solve all of the Date related issues.

Here is the example sample:

DateTimeFormatter fromFormat = DateTimeFormat.forPattern("dd/MM/yyyy"); // String pattern of your DOB from which you want to create DateTime Object
DateTime dob = fromFormat.parseDateTime(jsonObj.getString("dob")); // This will give you DateTime Object 
DateTimeFormatter toFormat = DateTimeFormat.forPattern("MMMM dd, yyyy"); // String pattern of your parsed DOB
myEditBox.setText(dob.toString(toFormat)); // April 2, 2016
myDatePickerDialog.getDatePicker().updateDate(dob.getYear());

Upvotes: -3

user6011162
user6011162

Reputation:

the Date.getYear(), getMonth() and getDay() are deprecated and specifically ask you to use Calendar.get()

Here is the relevant note from the API documentation

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getYear%28%29

I have used this code :

private void setDateTimeField(){
        usereditbirthdateedittext.setOnClickListener(this);

        Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);

                SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
                usereditbirthdateedittext.setText(dateFormatter.format(newDate.getTime()));

                selectedDate = new Date(newDate.getTimeInMillis());
            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    }

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317828

You're actually calling methods on Date, which are deprecated. (myCalendar.getDate() returns a Date object).

On an instance of Calendar, you can use get() and pass it constants to get year, month, date, and more (refer to the linked docs for get()).

Upvotes: -1

Related Questions