시간 차이를 계산하고 싶습니다. 세 가지 EditText가 있습니다. HH : MM 형식의 처음 두 편집 텍스트에 시간을 입력하고 싶습니다. 그리고 나서 시차를 계산하면 같은 형식으로 세 번째 edittext 필드에 결과가 표시됩니다. 날짜가 변경되면, 시간 차이, 즉있어서 계산할 즉날짜 변경과 관련하여 두 시간 필드 사이의 시간 차이를 계산하는 방법
경우 처음 = 23:00 번째 = 다음
1시, 시간차 = 2시 시간
public class TimeCalculate extends Activity {
private String mBlock;
private String mBlockoff;
private String mBlockon ;
// String mHours, mMinutes;
Date date1, date2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText blockoff = (EditText) findViewById(R.id.blockoff);
mBlockoff = blockoff.getText().toString();
EditText blockon = (EditText) findViewById(R.id.blockon);
mBlockon = blockon.getText().toString();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
try {
date1 = simpleDateFormat.parse(mBlockoff);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
date2 = simpleDateFormat.parse(mBlockon);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mBlock = getDifference(date1, date2);
EditText block = (EditText) findViewById(R.id.block);
block.setText(mBlock.toString());
}
public static String getDifference(Date startTime, Date endTime) {
if (startTime == null)
return "corrupted";
Calendar startDateTime = Calendar.getInstance();
startDateTime.setTime(startTime);
Calendar endDateTime = Calendar.getInstance();
endDateTime.setTime(endTime);
long milliseconds1 = startDateTime.getTimeInMillis();
long milliseconds2 = endDateTime.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
/*int hours = (int)diff/(60 * 60 * 1000);
int minutes = (int) (diff/(60 * 1000)); minutes = minutes - 60 * hours;
long seconds = diff/(1000); */
//timeDiff = DateUtils.formatElapsedTime(seconds);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM");
Date date = new Date(diff);
return simpleDateFormat.format(date);
}
}
는이 코드를 실행,하지만 찿아가 getDifference method.Please에서 오류가 다음 코드에 의해 계산된다 다른 논리를
그렇게하지,
Date
객체 에러에 대해 알고 있지만, 새로운 SimpleDateFormat ("hh : mm")'과 새로운 SimpleDateFormat ("HH : MM")'; 당신은 아마도 ""HH : mm "'을 원할 것이다. –