gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:47 27 4
gpt4 key购买 nike

我一直在尝试创建一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com