2017-03-25 16 views
7

Części mojej starej aplikacji są przestarzałe, próbuję ją odbudować. Zasadniczo jest to kalendarz z widokiem miesiąca. To jest część mojego gridview adaptera:GridView do widoku kalendarza

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(); 
} 

i int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); są przestarzałe. Próbuję użyć klasy Calendar zamiast Date, ale nie mogę zrobić tego samego. Wiem, że dla odzyskać dzień, miesiąc lub rok mogę użyć tego:

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

ale nie wiem jak przekonwertować ten wiersz:

Date date = getItem(position); 

za korzystanie z Calendar.

Odpowiedz

1

Oto jak przekonwertować Date obiektu do Calendar obiektu:

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

Potem (jak powiedziałeś) można zrobić:

int day = cal.get(Calendar.DAY_OF_MONTH); 
int month = cal.get(Calendar.MONTH) 
int year = cal.get(Calendar.YEAR); 
+0

Ok dzięki, więc nie można zrobić „natywnie” bez korzystania z obiektu Date tylko z przedmiotu kalendarzu? –

+0

Brak prob. Jeśli nie chcesz używać 'Date', musisz zmienić zestaw danych adaptera na' Calendar' i zrobić 'getItem()' return 'Calendar'. – 345

2

Można użyć tego wiersza kodu:

Po prostu zamień tę linię

Date date = getItem(position); 

z tej linii:

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

Oto pełna dla was przykładem:

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

pierwsze masz zamiar chcą odwołać Kalendarz. Gdy już to zrobisz, możesz powiedzieć 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

Szukasz odpowiedzi czerpiąc z wiarygodnym i/lub oficjalnych źródeł.

Ok

Do głównych źródeł:

  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 nie jest przestarzała. Tylko niektóre metody.

Więc

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 
} 
0

Oto przykładowy kod przekonwertować z Date do 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); 
}