我正在编写一个应用程序,我需要从开始时间选择器获取用户设置时间,以及用户从结束时间选择器设置时间。然后将开始时间和结束时间与当前时间进行比较。如果当前时间等于开始时间,则静音电话。如果结束时间等于当前时间,请将手机静音。任何帮助,特别是代码示例将不胜感激。提前谢谢。
public TimePickerDialog.OnTimeSetListener sTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
sHour = hourOfDay;
sMinute = minute;
updateDisplay();
displayToast();
}
};
public TimePickerDialog.OnTimeSetListener eTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
eHour = hourOfDay;
eMinute = minute;
updateDisplayE();
displayToastE();
}
};
public void updateDisplay() {
startTime.setText(new StringBuilder().append(pad(sHour)).append(":")
.append(pad(sMinute)));
final int compareStartHour = sHour;
Log.e("beginning", "Value " + sHour);
}
public void updateDisplayE()
{
endTime.setText(new StringBuilder().append(pad(eHour)).append(":")
.append(pad(eMinute)));
}
private void displayToast() {
Toast.makeText(
this,
new StringBuilder().append("Time choosen is ").append(
startTime.getText()), Toast.LENGTH_SHORT).show();
}
private void displayToastE() {
Toast.makeText(
this,
new StringBuilder().append("Time choosen is ").append(
endTime.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preset_edit);
save = (Button) findViewById(R.id.btnSave);
TextView eventTxt = (TextView) findViewById(R.id.txtViewEvent);
startTime = (Button) findViewById(R.id.btnStartTime);
endTime = (Button) findViewById(R.id.btnEndTime);
startTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
endTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
}
});
答案 0 :(得分:4)
使用Calendar
类实例:
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR, tPicker.getCurrentHour());
calendar.set(Calendar.MINUTE, tPicker.getCurrentMinute());
calendar.set(Calendar.SECOND, tPicker.getCurrentSecond());
//you can also set the day, month, year etc., default values are today's
然后致电
calendar.getTimeInMillis()
将以毫秒(long
)返回时间值,您可以轻松地将其与另一个进行比较。
有关详细信息,请参阅Calendar
课程文档。
答案 1 :(得分:1)
从TimePicker获取时间并保存:
Time selectedTime = new Time(sHour, sMinute, 0);
将其与当前时间或任何其他时间进行比较:
Date time = MyClassName.convertStringToTime(selectedTime.toString());
并将两个日期值传递给函数:
public static int compareTime(Date date1, Date date2) {
int result = date1.compareTo(date2);
if (result > 0) //
{
return 1;
} else if (result == 0) {
return 0;
}
return -1;
}
编辑: public static class MyClassName { public static Date convertStringToTime(String strTime) { System.out.println(“转换时间:”+ strTime +“。”); Date dateTobeReturned = null;
DateFormat sdf = new SimpleDateFormat(DateUtility.TIME_FORMAT);
try {
dateTobeReturned = sdf.parse(strTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Time: " + sdf.format(dateTobeReturned));
return dateTobeReturned;
}
}
请问您是否有任何疑问。希望它有所帮助。
答案 2 :(得分:1)
你也可以尝试这个..它对我有用...首先从TimePickerDialog中抽出一段时间,然后将其变为String,然后应用以下代码..
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
java.util.Date d_min = null;
java.util.Date d_max = null;
try {
d_min = sdf.parse(time_min);//time_min is the Minimum time in String
d_max = sdf.parse(time_max);time_max is the Maximun time in String
d_compare = d_max.compareTo(d_min);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (d_compare < 0) {
check = 3;
showDialog(0);
}
你可以像这样比较当前时间..希望这会对你有帮助.. 你也可以看一下...... current time
这...... link