如何在android中比较当前时间和所选时间
我有时间选择器,我不想从中选择上一次 这意味着,如果我在当前时间之前选择时间,它将无法显示。我不想在当前时间之前显示时间如何在android中比较当前时间和所选时间,android,validation,time,android-timepicker,Android,Validation,Time,Android Timepicker,我有时间选择器,我不想从中选择上一次 这意味着,如果我在当前时间之前选择时间,它将无法显示。我不想在当前时间之前显示时间 public class ProfileDetail extends AppCompatActivity { Toolbar toolbar; String description, str_requirement, str_date, str_time, str_noofperson, str_address; TextView txt_descri
public class ProfileDetail extends AppCompatActivity {
Toolbar toolbar;
String description, str_requirement, str_date, str_time, str_noofperson, str_address;
TextView txt_description;
EditText et_requirment, date_pick, time_set, et_no_of_person, et_address_service;
Button btn_submit;
int mYear, mMonth, mDay, mHour, mMinute;
private Calendar cal;
DateFormat fmtDateAndTime = DateFormat.getTimeInstance();
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_details);
Bundle extras = getIntent().getExtras();
description = extras.getString("description");
txt_description = (TextView) findViewById(R.id.txtservicedetails);
toolbar = (Toolbar) findViewById(R.id.toolbarforprofile);
setSupportActionBar(toolbar);
cal = Calendar.getInstance();
mDay = cal.get(Calendar.DAY_OF_MONTH);
mMonth = cal.get(Calendar.MONTH);
mYear = cal.get(Calendar.YEAR);
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
et_requirment = (EditText) findViewById(R.id.editText_requirement);
et_no_of_person = (EditText) findViewById(R.id.et_noofperson);
et_address_service = (EditText) findViewById(R.id.et_serviceaddress);
time_set = (EditText) findViewById(R.id.et_time);
date_pick = (EditText) findViewById(R.id.et_dateof_visit);
btn_submit = (Button) findViewById(R.id.btn_submit);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setTitle("Need-Hlp");
toolbar.setTitleTextColor(Color.WHITE);
txt_description.setText(description);
//date_pick.setInputType(InputType.TYPE_NULL);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentfromprevious = new Intent(ProfileDetail.this, NavigationDrawer.class);
intentfromprevious.putExtra("description", description);
intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentfromprevious);
}
});
date_pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
final TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
updateLabel();
}
};
time_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get Current Time
new TimePickerDialog(ProfileDetail.this, t, cal
.get(Calendar.HOUR_OF_DAY), cal
.get(Calendar.MINUTE), true).show();
}
});
//final TextView textView = (TextView)findViewById(R.id.textview23);
//////Submit button onclicklistener
btn_submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
str_date = date_pick.getText().toString();
if (et_requirment.getText().toString().equals("")) {
et_requirment.setError("Requirement cannot be blank");
et_requirment.requestFocus();
return;
}
if (str_date.isEmpty() || str_date.length() == 0 || str_date.equals("") || str_date == null) {
Toast.makeText(ProfileDetail.this, "Date cannot be blank", Toast.LENGTH_SHORT).show();
//date_pick.setError("Date cannot be blank");
return;
}
if (time_set.getText().toString().equals("")) {
Toast.makeText(ProfileDetail.this, "Time cannot be blank", Toast.LENGTH_SHORT).show();
//time_set.setError("Time cannot be blank");
//edittext_phone.requestFocus();
return;
}
if (et_no_of_person.getText().toString().toString().equals("")) {
et_no_of_person.setError("No of person cannot be blank");
//edittext_phone.requestFocus();
return;
}
if (et_address_service.getText().toString().equals("")) {
et_address_service.setError("Address cannot be blank");
//edittext_Password.requestFocus();
return;
}
if (!et_requirment.getText().toString().trim().equals("") && !et_no_of_person.getText().toString().trim().equals("") && !et_address_service.getText().toString().equals("")) {
Toast.makeText(ProfileDetail.this, "You Request Submitted", Toast.LENGTH_SHORT).show();
Intent intentforlist = new Intent(ProfileDetail.this, NavigationDrawer.class);
intentforlist.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentforlist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentforlist.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentforlist);
}
}
});
}
private void updateLabel() {
Calendar c = Calendar.getInstance();
String time = "23:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date timeCompare = null;
Date currentTimes = new Date();
int comareResult = timeCompare.compareTo(currentTimes);
try {
if (comareResult == 0) {
timeCompare = sdf.parse(time);
time_set.setText(time);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
//return new DatePickerDialog(this, datePickerListener, mYear, mMonth, mDay);
DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, mYear, mMonth, mDay) {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (year < mYear)
view.updateDate(mYear, mMonth, mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear, mMonth, mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear, mMonth, mDay);
}
};
return _date;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
date_pick.setText(+selectedDay + " / " + (selectedMonth + 1) + " / "
+ selectedYear);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item1 = menu.findItem(R.id.action_cart);
MenuItemCompat.setActionView(item1, R.layout.count);
// notificationCount1 = (RelativeLayout) MenuItemCompat.getActionView(item1);
return super.onCreateOptionsMenu(menu);
}
}
公共类ProfileDetail扩展了AppCompative活动{
工具栏;
字符串描述、str_要求、str_日期、str_时间、str_noofperson、str_地址;
文本视图txt_描述;
编辑文本设置要求、日期选择、时间设置、人员编号、地址服务;
按钮btn_提交;
迈耶、蒙斯、迈迪、迈乌尔、迈努特;
私人日历cal;
DateFormat fmtDateAndTime=DateFormat.getTimeInstance();
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_详细信息);
Bundle extras=getIntent().getExtras();
description=extras.getString(“description”);
txt_description=(TextView)findViewById(R.id.txtservicedetails);
toolbar=(toolbar)findViewById(R.id.toolbarforprofile);
设置支持操作栏(工具栏);
cal=Calendar.getInstance();
mDay=cal.get(日历日期,每月的第天);
MONTH=cal.get(日历月);
mYear=cal.get(日历年);
mHour=cal.get(日历小时);
mMinute=cal.get(日历分钟);
et_需求=(EditText)findViewById(R.id.EditText_需求);
et_no_of_person=(EditText)findViewById(R.id.et_noofperson);
et_地址\服务=(EditText)findViewById(R.id.et_服务地址);
时间集=(编辑文本)findViewById(R.id.et_时间);
日期选择=(编辑文本)findViewById(R.id.et\u访问日期);
btn_submit=(按钮)findviewbyd(R.id.btn_submit);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
工具栏.setTitle(“需要Hlp”);
toolbar.setTitleTextColor(Color.WHITE);
txt_description.setText(说明);
//日期\拾取.设置输入类型(输入类型.类型\空);
toolbar.setNavigationOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent intentfromprevious=新意图(ProfileDetail.this,NavigationDrawer.class);
意图来自previous.putExtra(“描述”,描述);
intentfromprevious.addFlags(Intent.FLAG\u活动\u新任务);
intentfromprevious.addFlags(Intent.FLAG\u活动\u清除\u任务);
intentfromprevious.addFlags(Intent.FLAG\u活动\u否\u动画);
StartTactivity(意向上一个);
}
});
date\u pick.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
显示对话框(0);
}
});
final TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener(){
时间集上的公共void(时间选择器视图,整数小时日,整数分钟){
校准设置(日历小时/天,小时/天);
校准设置(日历、分钟、分钟);
updateLabel();
}
};
time\u set.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//获取当前时间
新的TimePickerDialog(ProfileDetail.this,t,cal
.get(日历、每天小时数),cal
.get(Calendar.MINUTE),true.show();
}
});
//最终文本视图文本视图=(文本视图)findViewById(R.id.textview23);
//////单击Listener上的Submit按钮
btn_submit.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v){
str_date=date_pick.getText().toString();
if(et_requirement.getText().toString().equals(“”){
et_Requirement.setError(“要求不能为空”);
et_requirement.requestFocus();
返回;
}
如果(str|u date.isEmpty()| str|u date.length()=0 | | str|u date.equals(“”| | str|u date==null){
Toast.makeText(ProfileDetail.this,“日期不能为空”,Toast.LENGTH_SHORT.show();
//日期_pick.setError(“日期不能为空”);
返回;
}
if(time_set.getText().toString()等于(“”){
Toast.makeText(ProfileDetail.this,“时间不能为空”,Toast.LENGTH_SHORT.show();
//time_set.setError(“时间不能为空”);
//edittext_phone.requestFocus();
返回;
}
if(et_no_of_person.getText().toString().toString().equals(“”){
设置人员编号。设置错误(“人员编号不能为空”);
//edittext_phone.requestFocus();
返回;
}
if(et_address_service.getText().toString().equals(“”){
et_address_service.setError(“地址不能为空”);
//edittext_Password.requestFocus();
返回;
}
如果(!et_requirement.getText().toString().trim().equals(“”&!et_no_of_person.getText().toString().trim().equals(“”&!et_address_service.getText().toString().equals(“”)){
Toast.makeText(ProfileDetail.this,“您的请求已提交”,Toast.LENGTH_SHORT.show();
Intent intentforlist=newintent(ProfileDetail.this,NavigationDrawer.class);
intentforlist.addFlags(Intent.FLAG\u活动\u新任务);
intentforlist.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TASK);
myDatePicker.setMinDate(System.currentTimeMillis(););
datePicker.setMinDate(System.currentTimeMillis() - 1000);