4
Calendar date = Calendar.getInstance();     
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");

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

我已经在 onCreate 方法中完成了上面的代码。但它只能返回当前日期。我想要的是在日历中选择日期

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

请帮我

4

5 回答 5

16

我用CalendarView而不是日历,它现在正在工作。

calendarView.setOnDateChangeListener(new OnDateChangeListener() {

                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    curDate = String.valueOf(dayOfMonth);
                }
            });
于 2013-04-16T07:59:10.727 回答
0

您可以像这样使用日历视图获取选定日期...

CalendarView calendarView=view.findViewById(R.id.calendarView);

    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {
          String  curDate = String.valueOf(dayOfMonth);
          String  Year = String.valueOf(year);
          String  Month = String.valueOf(month);

            Log.e("date",Year+"/"+Month+"/"+curDate);
        }
    });
于 2021-02-10T12:36:50.547 回答
0

Kotlin获取选定日期的示例代码CalenderView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast Selected Date" />

</LinearLayout>

MainActivity.kt

import android.os.Bundle
import android.text.format.DateFormat
import android.util.Log
import android.widget.CalendarView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.testandcheck.databinding.ActivityMainBinding
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding   // Binding object for ViewBinding.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Set date change listener on calenderView.
    // Callback notified when user select a date from CalenderView on UI.
    binding.calendarView.setOnDateChangeListener { calView: CalendarView, year: Int, month: Int, dayOfMonth: Int ->

        // Create calender object with which will have system date time.
        val calender: Calendar = Calendar.getInstance()

        // Set attributes in calender object as per selected date.
        calender.set(year, month, dayOfMonth)

        // Now set calenderView with this calender object to highlight selected date on UI.
        calView.setDate(calender.timeInMillis, true, true)
        Log.d("SelectedDate", "$dayOfMonth/${month + 1}/$year")
    }

    // Example button to show get selected date from CalenderView and show in Toast.
    binding.button.setOnClickListener {

        // Fetch long milliseconds from calenderView.
        val dateMillis: Long = binding.calendarView.date

        // Create Date object from milliseconds.
        val date: Date = Date(dateMillis)

        // Get Date values and created formatted string date to show in Toast.
        val selectedDayOfWeek = DateFormat.format("EEEE", date) as String // Monday
        val selectedDay = DateFormat.format("dd", date) as String // 05
        val selectedMonthString = DateFormat.format("MMM", date) as String // Jul
        val selectedMonthNumber = DateFormat.format("MM", date) as String // 6 --> Month Code as Jan = 0 till Dec = 11.
        val selectedYear = DateFormat.format("yyyy", date) as String // 2021

        val strFormattedSelectedDate = "$selectedDay-${selectedMonthNumber + 1}-$selectedYear ($selectedDayOfWeek)"
        Toast.makeText(applicationContext, "Selected Date = $strFormattedSelectedDate", Toast.LENGTH_SHORT).show()
    }
}

}

说明:

  1. 设置侦听器以设置所选日期CalenderView(同时突出显示新选择的日期并取消突出显示较旧的日期)。
  2. 然后只调用calenderView.date以获取选定的日期。
于 2021-07-05T08:16:42.300 回答
-1

你试过 Calendar.get(Calendar.DATE)

检查链接GetDate() 日历

于 2013-04-16T07:46:15.503 回答
-1
    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()));
于 2017-03-23T14:07:47.217 回答