我正在尝试根据用户从选择器中选择的日期和时间创建结束时间。有没有人知道这是否可以计算或使用从选择器中选择的日期和时间?
这是已尝试的代码..它目前获取的当前日期和时间不是用户从选择器中选择的。
以下是采摘者:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_line);
myDb = new DatabaseHelper(this);
quantity = (TextView) findViewById(R.id.editText3);
duration = (TextView) findViewById(R.id.durationtextView);
dateButton = (Button) findViewById(R.id.angry_btn);
VerifyButton = (Button) findViewById(R.id.confirmButton);
tv = (Button) findViewById(R.id.buttonddate);
mCurrentDate = Calendar.getInstance();
day = mCurrentDate.get(Calendar.DAY_OF_MONTH);
month = mCurrentDate.get(Calendar.MONTH);
year = mCurrentDate.get(Calendar.YEAR);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
monthOfYear = monthOfYear +1;
String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);
tv.setText(Sd);
}
}, year, month, day);
datePickerDialog.show();
}
});
display = (Button) findViewById(R.id.button_displayTPD);
Button displayTDPButton = (Button) findViewById(R.id.button_displayTPD);
displayTDPButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new TimePickerDialog(CreateLine.this, onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
}
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String St = String.valueOf(hourOfDay + ":" + minute);
display.setText(St);
}
};
这是结束时间的计算
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dateButton.setText(date.toString());
// Creates an instance of current DateTime which represents the
// current date time.
//DateTime dateTime = new DateTime();
DateTime dateTime = new DateTime(year, month, day, 12, 0, 0, 11);
DateTimeFormatter fmt = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");
String formattedtime = fmt.print(dateTime);
dateButton.setText(formattedtime);
// Plus some hours, minutes, and seconds to the original DateTime.
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");
DateTime dateTime1 = dateTime.plusHours(timeadded);
String endtimecalc = fmt2.print(dateTime1);
TextView endtime = (TextView) findViewById(endtimetextView);
endtime.setText(endtimecalc);
}
});
VerifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String spinnerSelection = String.valueOf(spinner.getSelectedItem());
String spinnerSelection2 = String.valueOf(spinner2.getSelectedItem());
String q = quantity.getText().toString();
String d = duration.getText().toString();
DateTime dateTime = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");
String formattedtime = fmt.print(dateTime);
dateButton.setText(formattedtime);
// Plus some hours, minutes, and seconds to the original DateTime.
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");
DateTime dateTime1 = dateTime.plusHours(timeadded);
String endtimecalc = fmt2.print(dateTime1);
TextView endtime = (TextView) findViewById(endtimetextView);
endtime.setText(endtimecalc);
这是预期结果'endtime'的屏幕截图。结果是拣货员选择的日期和时间加上小时的持续时间应该=结束时间
答案 0 :(得分:0)
首先将日期转换为毫秒日期,然后计算时间
Date startDate = // Set start date
Date endDate = // Set end date
long duration = endDate.getTime() - startDate.getTime();
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);