Android获取当前Calendarview的年,月

时间:2018-11-12 19:58:35

标签: android calendar calendarview

在日历视图中,起初我可以设置日期,但是当我单击两个将日历更改为上个月或下个月的按钮时,我无法获得哪个月份和年份。 而且,calendarview支持滑动到上个月或下个月, 但是我无法获取现在显示日历的年份和月份信息。 如何获取查看(现在显示)日历的年和月信息?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

 final DatePickerDialog.OnDateSetListener dateto = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                updateLabe2();


            }

        };
        To.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), dateto, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
  public void updateLabe2()
{
String myFormat = "yyyy-MM-dd"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    To.setText(sdf.format(myCalendar.getTime()));
//here To is editbox
}