2017-03-25 14 views
7

내 이전 응용 프로그램의 일부가 사용되지 않으므로 다시 작성하려고합니다. 기본적으로 월별보기가있는 달력입니다.캘린더보기 용 GridView

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

int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); 가 사용되지 않습니다 이 내있는 gridview 어댑터의 일부입니다. Date 대신 Calendar 클래스를 사용하려고하지만 동일한 작업을 수행 할 수 없습니다. 나는 일, 월 또는 연도 검색을 위해 내가 이것을 사용할 수 있다는 것을 알고 :

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

하지만 난이 줄을 변환하는 방법을 알고하지 않습니다

Date date = getItem(position); 

Calendar와 함께 사용하는. (당신이 말한 것처럼) 그런 다음

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

당신이 할 수 있습니다 :

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

답변

1

당신이 Calendar 객체에 Date 개체를 변환하는 방법입니다

이 줄 바꾸기

Date date = getItem(position); 
이 라인 6,

: 여기

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

당신을위한 완벽한 예입니다

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); 
+0

감사합니다. 그렇기 때문에 Calendar 객체에서만 Date 객체를 사용하지 않고 "기본적으로"수행 할 수 없습니까? –

+0

문제가 없습니다. 'Date'를 사용하고 싶지 않다면 어댑터 데이터 셋을'Calendar'로 변경하고'getItem()'을'Calendar'를 리턴해야합니다. – 345

2

당신은 코드 줄을 사용할 수 있습니다 : 여기

1

먼저 당신이 거 달력을 참조 할 것. 당신이 그 일을 한 후에는 신뢰할 수 및/또는 공식 소스에서 그리기 답을 찾고 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

말할 수 있습니다. 주요 소스에

확인

:

  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이되지되지 않습니다. 일부 방법 만.

그래서,

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

여기 Date에서 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); 
}