英文:
Subtract one hour from another
问题
以下是翻译好的代码部分:
hIni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = calendario.get(Calendar.HOUR_OF_DAY);
int minute = calendario.get(Calendar.MINUTE);
final TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
calendario.set(Calendar.HOUR_OF_DAY, hour);
calendario.set(Calendar.MINUTE, minute);
horaInicio = String.format("%02d:%02d", hour, minute);
hIni.setText(horaInicio);
if (!horaFin.equals(""))
{
if (testChar(hFin.toString()))
{
Date hI = ParseHora(horaInicio);
Date hF = ParseHora(horaFin);
//obtienes la diferencia de las fechas
long difference = Math.abs(hF.getTime() - hI.getTime());
//obtienes la diferencia en minutos ya que la diferencia anterior esta en milisegundos
difference= difference / (60 * 1000);
horasDur=difference/60;
minDur = difference%60;
dur.setText(horasDur +" horas "+minDur+" minutos");
}
}
}
};
new TimePickerDialog(NueAvaCuando.this,time, hour, minute, true).show();
}
});
hFin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = calendario.get(Calendar.HOUR_OF_DAY);
int minute = calendario.get(Calendar.MINUTE);
final TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
calendario.set(Calendar.HOUR_OF_DAY, hour);
calendario.set(Calendar.MINUTE, minute);
horaFin = String.format("%02d:%02d", hour, minute);
hFin.setText(horaFin);
if (!horaInicio.equals(""))
{
if (testChar(hIni.toString()))
{
Date hI = ParseHora(horaInicio);
Date hF = ParseHora(horaFin);
//obtienes la diferencia de las fechas
long difference = Math.abs(hF.getTime() - hI.getTime());
//obtienes la diferencia en horas ya que la diferencia anterior esta en milisegundos
difference= difference / (60 * 60 * 1000);
horasDur=difference/60;
minDur = difference%60;
dur.setText(horasDur +"horas "+minDur+" minutos");
}
}
}
};
new TimePickerDialog(NueAvaCuando.this,time, hour, minute, true).show();
}
});
方法部分:
public static Date ParseHora(String fecha)
{
SimpleDateFormat formato = new SimpleDateFormat("HH:mm");
Date fechaDate = null;
try {
fechaDate = formato.parse(fecha);
}
catch (ParseException ex)
{
System.out.println(ex);
}
return fechaDate;
}
public boolean testChar(String cadena) {
if (!Character.isDigit(cadena.charAt(0)))
return true;
else
return false;
}
希望这对你有帮助。
英文:
I have two hours, in the format (HH: mm) that I have passed to Date to subtract.
By subtracting them, I get the difference in milliseconds, and I convert them to hours dividing by 3600, and to minutes, first dividing by 60, and then doing the% operation.
If, for example, I put the starting time 20:00 and the final time 22:00, I get 0 hours and 2 minutes as a result.
Does anyone see the error?
This is my code:
hIni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = calendario.get(Calendar.HOUR_OF_DAY);
int minute = calendario.get(Calendar.MINUTE);
final TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
calendario.set(Calendar.HOUR_OF_DAY, hour);
calendario.set(Calendar.MINUTE, minute);
horaInicio = String.format("%02d:%02d", hour, minute);
hIni.setText(horaInicio);
if (!horaFin.equals(""))
{
if (testChar(hFin.toString()))
{
Date hI = ParseHora(horaInicio);
Date hF = ParseHora(horaFin);
//obtienes la diferencia de las fechas
long difference = Math.abs(hF.getTime() - hI.getTime());
//obtienes la diferencia en minutos ya que la diferencia anterior esta en milisegundos
difference= difference / (60 * 1000);
horasDur=difference/60;
minDur = difference%60;
dur.setText(horasDur +" horas "+minDur+" minutos");
}
}
}
};
new TimePickerDialog(NueAvaCuando.this,time, hour, minute, true).show();
}
});
hFin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = calendario.get(Calendar.HOUR_OF_DAY);
int minute = calendario.get(Calendar.MINUTE);
final TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
calendario.set(Calendar.HOUR_OF_DAY, hour);
calendario.set(Calendar.MINUTE, minute);
horaFin = String.format("%02d:%02d", hour, minute);
hFin.setText(horaFin);
if (!horaInicio.equals(""))
{
if (testChar(hIni.toString()))
{
Date hI = ParseHora(horaInicio);
Date hF = ParseHora(horaFin);
//obtienes la diferencia de las fechas
long difference = Math.abs(hF.getTime() - hI.getTime());
//obtienes la diferencia en horas ya que la diferencia anterior esta en milisegundos
difference= difference / (60 * 60 * 1000);
horasDur=difference/60;
minDur = difference%60;
dur.setText(horasDur +"horas "+minDur+" minutos");
}
}
}
};
new TimePickerDialog(NueAvaCuando.this,time, hour, minute, true).show();
}
});
here the methods:
public static Date ParseHora(String fecha)
{
SimpleDateFormat formato = new SimpleDateFormat("HH:mm");
Date fechaDate = null;
try {
fechaDate = formato.parse(fecha);
}
catch (ParseException ex)
{
System.out.println(ex);
}
return fechaDate;
}
public boolean testChar(String cadena) {
if (!Character.isDigit(cadena.charAt(0)))
return true;
else
return false;
}
thank you very much
答案1
得分: 3
问题出在这里,在 onTimeSet
方法内部,该方法在传递给 hFin.setOnClickListener
的监听器中定义:
difference= difference / (60 * 60 * 1000);
在执行这条语句之前,difference
的单位是毫秒。在这之后的代码期望的是分钟。然而,通过除以 1000
将毫秒转换为秒,因此除以 60 * 1000
已经得到了分钟,所以除以 60 * 60 * 1000
得到的是小时,这不是你想要的。
代码中的那部分应该是:
difference = difference / (60 * 1000);
英文:
The problem is here, inside the onTimeSet
method, defined inside the listener passed to hFin.setOnClickListener
:
difference= difference / (60 * 60 * 1000);
Before this statement is executed, difference
is in milliseconds. The code after this is expecting minutes. However, dividing by 1000
converts milliseconds to seconds, and therefore dividing by 60 * 1000
already gets you to minutes, so dividing by 60 * 60 * 1000
converts to hours, which is not what you want.
That part of the code should be:
difference = difference / (60 * 1000);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论