2013-04-16 32 views
0
Calendar date = Calendar.getInstance();  
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd"); 

curDate = sdf.format(date.getTime()); 

我在onCreate方法裏面做了上面的代碼。但它只能返回當前日期。我想要的是在日曆中選擇的日期。如何在Android中的CalenderView中獲取選定的日期?

<CalendarView 
     android:id="@+id/calendarView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

請幫我

+1

讀取文檔,存在CalendarView一個GETDATE方法。你的代碼是關於日曆的,如果你閱讀文檔,你會看到完全不同的類。 – njzk2

回答

9

,它的工作現在。

calendarView.setOnDateChangeListener(new OnDateChangeListener() { 

       @Override 
       public void onSelectedDayChange(CalendarView view, int year, int month, 
         int dayOfMonth) { 
        int d = dayOfMonth; 
        curDate =String.valueOf(d); 
       } 
      }); 
-1

你嘗試Calendar.get(Calendar.DATE)

檢查我用CalendarView,而不是日曆鏈接GetDate() Calendar

+0

我使用了CalendarView而不是Calender。感謝您的幫助 – Ravindu

+1

我不明白爲什麼downvote? –

0
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); 
    final DateFormat dateFormat_year = new SimpleDateFormat("yyyy"); 
    final DateFormat dateFormat_month = new SimpleDateFormat("MM"); 
    final DateFormat dateFormat_day = new SimpleDateFormat("dd"); 
    Calendar cal = Calendar.getInstance(); 

    System.out.println(dateFormat.format(cal.getTime())); 
    System.out.println(dateFormat_year.format(cal.getTime())); 
    System.out.println(dateFormat_month.format(cal.getTime())); 
    System.out.println(dateFormat_day.format(cal.getTime())); 
相關問題