5

Parts of my old app are deprecated, I am trying to rebuild it. Basically it is a Calendar with month view. This is a part of my gridview adapter:

public View getView(int position, View view, ViewGroup parent) {
    Date date = getItem(position);
    int day = date.getDate();
    int month = date.getMonth();
    int year = date.getYear();
}

and the int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); are deprecated. I am trying to use the Calendar class instead of Date but unable to do the same. I know that for retrieve the day, month or year I can use this:

Calendar calendar = Calendar.getInstance();
calendar.get(Calendar.DAY_OF_MONTH); 

but I don't know how to convert this line:

Date date = getItem(position);

for using with Calendar.

Adil Saiyad
  • 1,582
  • 2
  • 17
  • 34
Dan NoNeed
  • 51
  • 3

5 Answers5

2

You can use this line of code:

Just replace this line

Date date = getItem(position);

with this line:

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();

Here is a full example for you:

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
int day =  calendar.get(Calendar.DAY_OF_MONTH);
int month =  calendar.get(Calendar.MONTH);
int year =  calendar.get(Calendar.YEAR);
AndiM
  • 2,196
  • 2
  • 21
  • 38
1

Here is how you convert a Date object to a Calendar object:

Calendar cal = Calendar.getInstance();
cal.setTime(date);

Then (like you said) you can do:

int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH)
int year = cal.get(Calendar.YEAR);
aneurinc
  • 1,238
  • 12
  • 20
  • Ok thanks, so it cannot done "natively" without using Date object only with Calendar object? – Dan NoNeed Mar 25 '17 at 11:17
  • No prob. If you don't want to use `Date` then you will need to change your adapter data set to `Calendar` and make `getItem()` return `Calendar`. – aneurinc Mar 25 '17 at 11:29
1

First you're gonna want to reference Calendar. Once you've done that, you can say Date date = calendar.getTime

public View getView(int position, View view, ViewGroup parent) {
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    int day =  calendar.get(Calendar.DAY_OF_MONTH);
    int month =  calendar.get(Calendar.MONTH)    
    int year =  calendar.get(Calendar.YEAR);
}
1

Looking for an answer drawing from credible and/or official sources.

Ok

To major sources:

  1. https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

  2. https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

Date is not deprecated. Only some methods.

So,

public View getView(int position, View view, ViewGroup parent) {

    Date date = getItem(position);
    long ms = date.getTime();https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime()
    Calendar calendar = Calendar.getInstance();//allowed
    calendar.setTimeInMillis(ms);//allowed https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTimeInMillis(long)
    //calendar.setTime(date); is also allowed https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date)
    int day =  calendar.get(Calendar.DAY_OF_MONTH);//allowed 
    int month =  calendar.get(Calendar.MONTH);//allowed  
    int year =  calendar.get(Calendar.YEAR);//allowed 
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Here is the sample code to convert from Date to Calendar.

public View getView(int position, View view, ViewGroup parent) {
    Date date = getItem(position);
    // convert a Date object to a Calendar object
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int day =  calendar.get(Calendar.DAY_OF_MONTH);
    int month =  calendar.get(Calendar.MONTH);
    int year =  calendar.get(Calendar.YEAR);
}
Curest0x
  • 71
  • 1
  • 7