2017-03-25 7 views
7

मेरे पुराने ऐप के हिस्सों को बहिष्कृत कर दिया गया है, मैं इसे पुनर्निर्माण करने की कोशिश कर रहा हूं। असल में यह महीने के दृश्य के साथ एक कैलेंडर है।कैलेंडर दृश्य के लिए ग्रिड व्यू

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 साथ प्रयोग करने के लिए।

उत्तर

1

यहाँ है कि कैसे आप एक Calendar ऑब्जेक्ट में एक Date वस्तु कन्वर्ट:

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

ठीक है, इसलिए यह केवल कैलेंडर ऑब्जेक्ट के साथ दिनांक वस्तु का उपयोग किए बिना "मूल रूप से" नहीं किया जा सकता है? –

+0

कोई जांच नहीं। यदि आप 'दिनांक' का उपयोग नहीं करना चाहते हैं तो आपको अपने एडाप्टर डेटा सेट को' कैलेंडर' में बदलना होगा और 'getItem() 'return' calendar' बनाना होगा। – 345

2

आप कोड की इस पंक्ति का उपयोग कर सकते हैं:

बस इस लाइन को प्रतिस्थापित करें

Date date = getItem(position); 

इस लाइन के साथ:

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