gpt4 book ai didi

java - 如何检查我选择了一周中的哪一天以及是否是周末

转载 作者:太空狗 更新时间:2023-10-29 16:34:11 24 4
gpt4 key购买 nike

我已经在日期选择器中编写了代码。它工作正常。现在我想添加一项功能。无论是工作日还是周末。我想要一些指南,说明需要使用哪种方法来检查我选择的日期以及它是周末还是工作日。拜托,任何人都可以检查我的代码并指导我。我们将不胜感激。提前致谢。

public class DateTimePickerActivity extends AppCompatActivity implements
View.OnClickListener,
TimePickerDialog.OnTimeSetListener


// TimePickerDialog.OnTimeSetListener
{
//Declaration for class
ButtonViews views;
dpListener dpListenerView;
// Declartion for member vairables
int day, month, x_year;
int hour;
int minute;
Calendar calendar;

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

views = new ButtonViews();
dpListenerView = new dpListener();

//ButtonListener
views.button_date.setOnClickListener(this);
views.button_time.setOnClickListener(this);
//

// pick up the default date using Calender class
calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());

day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);

hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);

setupDate(day, month, x_year);
setupTime(hour, minute);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDatePickerDialog();
break;
case R.id.button_time:
showTimePickerDialog();
break;
}
}


private void setupTime(int hours, int minutes) {
views.button_time.setText(hours + ":" + minutes);
}

private void setupDate(int day, int month, int year) {

String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
}
private void showDatePickerDialog() {
DatePickerDialog datepickerdialog = new DatePickerDialog
(
this,
dpListenerView,
/* new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
} },*/
//this,

x_year,
month,
day
);

datepickerdialog.show();
}


/* private OnDateSetListener dpListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
*//* day = dayOfMonth;
month = monthOfYear;
x_year = year;*//*
setupDate(dayOfMonth,monthOfYear,year);
}

};*/


public void showTimePickerDialog() {

TimePickerDialog timePickerDialog = new TimePickerDialog(
DateTimePickerActivity.this,
this,
hour,
minute,
true
);


timePickerDialog.show();
}

// @Override
// public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// setupDate(dayOfMonth,monthOfYear,year);
// }

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setupTime(hourOfDay, minute);
}


class ButtonViews {

Button button_time;
Button button_date;

public ButtonViews() {
button_date = (Button) findViewById(R.id.button_date);
button_time = (Button) findViewById(R.id.button_time);
}
}

class dpListener implements OnDateSetListener
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
month = monthOfYear;
x_year = year;

int i = calendar.get(Calendar.DAY_OF_WEEK);
switch (i) {
case 1:
Toast.makeText(DateTimePickerActivity.this
, "You have selected " + calendar.SUNDAY
, Toast.LENGTH_SHORT);
break;

default:
setupDate(dayOfMonth,monthOfYear,year);
}
}

}
}

最佳答案

试试这段代码

Calendar yourCalendar;

int dayOfWeek = yourCalendar.get(Calendar.DAY_OF_WEEK); // it will returns 1 (Calendar.SUNDAY) to 7 (Calendar.SATURDAY)

那你应该检查一下

If(dayOfWeek == 7 || dayOfWeek == 1){
// as your requirement: you should display mesage they can not select the weekend" here
// then you set the value in datepickerdialog by current date
return "weekend";
}else{
return "week date";
}

关于java - 如何检查我选择了一周中的哪一天以及是否是周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961537/

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