0
저는 Android가 처음이므로 작은 코드를 작성하여 연습하려고합니다. 날짜에 차이를 가져오고 텍스트보기를 기반으로 표시하려고합니다. 그러나 나는 안드로이드 전화에서 같은 날에 다른 차이를 얻고있다. 내가 응용 프로그램을 열 때처럼, 그것은 일의 차이가 5라고 말합니다. 나는 그것을 닫고 다시 열어, 이제는 diff가 4라고 말합니다!Android에서 날짜의 정확하지 않은 차이점
에뮬레이터에서 제대로 작동합니다. 다음 코드를 사용하고 있습니다. 누구든지 pls가 약간의 개선을 제안 할 수 있습니까?
package com.practice.tablet;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TabletsSchedule extends Activity {
public TextView tvToday;
public TextView tvMedStartDate;
public TextView tvMessage;
public TextView tvDiffInDays;
public static final String LOGGER = "ADITYA";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvToday=(TextView)findViewById(R.id.TextView01);
tvMedStartDate=(TextView)findViewById(R.id.TextView001);
tvMessage=(TextView)findViewById(R.id.TextView05);
tvDiffInDays=(TextView)findViewById(R.id.TextView06);
String[] months = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
Calendar today = Calendar.getInstance();
int day= today.get(Calendar.DAY_OF_MONTH);
int month=today.get(Calendar.MONTH);
int year=today.get(Calendar.YEAR);
String strMonth = months[month];
Log.d(LOGGER, "displaying today");
tvToday.setText("Today is "+day+" "+strMonth+" "+year);
Calendar medStartDate = Calendar.getInstance();
//DOB.set(year, monthOfYear+1, dayOfMonth);
medStartDate.set(Calendar.DAY_OF_MONTH, 27);
medStartDate.set(Calendar.MONTH,10);
medStartDate.set(Calendar.YEAR,2011);
int day1= medStartDate.get(Calendar.DAY_OF_MONTH);
int month1=medStartDate.get(Calendar.MONTH);
int year1=medStartDate.get(Calendar.YEAR);
String strMonth1 = months[month1];
//String med = medStartDate.toString().toString();
tvMedStartDate.setText("Medicine Start Date is "+day1+" "+strMonth1+" "+year1);
long diff = today.getTimeInMillis() - medStartDate.getTimeInMillis();
long days = diff/(24 * 60 * 60 * 1000);
tvDiffInDays.setText("Difference in days is "+days);
Log.d(LOGGER, "days is "+days);
if (days%2!=0){
tvMessage.setText("Today you have to take two tablets");
}
if (days%2==0){
tvMessage.setText("Today you have to take one tablet");
}
}
}
감사합니다. Hiral! med 시작 날짜를 지우도록 코드를 수정했는데 이제 예상대로 작동하는 것 같습니다. 나는 그것이 매우 이상하게 행동하는 방법에 매우 놀랐다! 하지만 지금은 모두 괜찮아요 :) – user1073326
그것은 또한 밀리 초의 긴 값으로 시간의 작은 차이를 계산하기 때문입니다. 필요한 값을 설정하고 비교하기 전에 두 캘린더 개체를 모두 지우면 초기 값이 비슷하게 양쪽 모두에 대해 기본값으로 설정됩니다. 특정 값을 설정 한 후에는 실제로 비교할 것입니다. – Hiral
@Hiral plz 내가 계산 한 두 날짜 사이의 차이점을 찾을 수 있도록 도와 주지만 액티 베이트 일에 대한 약간의 변동을 보여줍니다 .... plz ... –