java - 从日期范围选择中选择第一个和最后一个日期

标签 java android calendar

我一直在尝试创建一个 Android 应用程序,它能够从日历中选择多天,并在不同的 TextView 中显示选择的第一个和最后一个日期,并在 Firestore 数据库中更新该事件。

这是日历 Activity 类 CalendarActivity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.squareup.timessquare.CalendarPickerView;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarActivity extends AppCompatActivity {

     String eventStartDate ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CalendarPickerView calendar_view = (CalendarPickerView) 
        findViewById(R.id.calendar_view);
            //getting currentS
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);
        Date today = new Date();



            //add one year to calendar from todays date
        calendar_view.init(today, nextYear.getTime())
                .inMode(CalendarPickerView.SelectionMode.RANGE);

        //action while clicking on a date
        calendar_view.setOnDateSelectedListener(new 
        CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {

                Toast.makeText(getApplicationContext(),"Selected Date is : " +date.toString(),Toast.LENGTH_SHORT).show();

                eventStartDate=date.toString();


                }



            @Override
            public void onDateUnselected(Date date) {

               //...
            }
        });

        //fetch dates

        final List<Date> dates = calendar_view.getSelectedDates();

        //final int eventEndDateIndex = dates.lastIndexOf(dates);
        //eventStartDate = dates.get(0);
        //eventEndDate= dates.get(eventEndDateIndex);



        //Displaying all selected dates while clicking on a button
        Button btn_show_dates = (Button) findViewById(R.id.btn_show_dates);
        btn_show_dates.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int j=0;

                for (int i = 0; i< calendar_view.getSelectedDates().size();i++){

                    //here you can fetch all dates
                    Toast.makeText(getApplicationContext(),calendar_view.getSelectedDates().get(i).toString(),Toast.LENGTH_SHORT).show();
                }

            }
        });


    }
}

这是布局文件 Activity_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CalendarActivity">

    <com.squareup.timessquare.CalendarPickerView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="467dp"
        android:layout_above="@+id/btn_show_dates"
        android:layout_alignParentTop="true"
        android:background="#FFFFFF"
        android:clipToPadding="false"
        android:scrollbarStyle="outsideOverlay" />

    <Button
        android:id="@+id/btn_show_dates"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Display Dates"
        android:background="@color/colorPrimary"
        android:textColor="#FFFFFF"/>

    <TextView
        android:id="@+id/eventStartDate"
        android:layout_width="107dp"
        android:layout_height="23dp"
        android:text="Event Start Date"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="472dp" />

    <TextView
        android:id="@+id/eventEndDate"
        android:layout_width="116dp"
        android:layout_height="23dp"
        android:text="Event End Date"
        tools:layout_editor_absoluteX="252dp"
        tools:layout_editor_absoluteY="472dp" />

</android.support.constraint.ConstraintLayout>

最佳答案

您有一个选定日期的列表作为日期对象:

calendar_view.getSelectedDates();

使用比较函数,例如:

           Collections.sort(your_dates, new Comparator<Date>() {
                @Override
                public int compare(Date d1, Date d2) {
                    if(d1.getTime() > d2.getTime())
                        return 1;
                    else if(d1.getTime() < d2.getTime())
                        return -1;
                    return 0;
                }
            });

然后您就可以访问列表的第一个和最后一个元素。

关于java - 从日期范围选择中选择第一个和最后一个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52355809/

相关文章:

java - HttpURLConnection OutputStream 问题

ios - 快速添加谷歌日历事件

java - KeyTyped 方法说 TextField 为空...但实际上不是

java - Neo4j 令人震惊的写入性能

java - 如何通过 RServe 将浮点/ double 组作为 vector 从 JAVA 发送到 R

android - ormlite:sql语句不像抛出异常

java - 如何在 RxJava2 中获取作为订阅者的主题的 Disposable

java - 多个 Actor 作为按钮 - 检测哪个 Actor 被触摸/点击

android - 在 Android 中按日期获取日历事件?

WORDPRESS:带有自定义帖子类型 + 自定义字段的日历