2011-12-02 82 views
0

我是Android新手,嘗試通過製作小代碼來練習。 我想在日期中獲得差異並基於它顯示文本視圖。 但我在Android手機當天獲得了不同的區別。 就像當我打開應用程序,它說在天差異是5. 我關閉它,重新打開它,現在它說diff是4!Android中的日期差異

在模擬器上它工作正常。 我正在使用以下代碼。 任何人都可以建議一些改進?

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"); 

     } 

    } 
} 

回答

1

使用此:

... 
Calendar medStartDate = Calendar.getInstance(); 

medStartDate.clear(); 

medStartDate.set(Calendar.DAY_OF_MONTH, 27); 
medStartDate.set(Calendar.MONTH,10); 
medStartDate.set(Calendar.YEAR,2011); 

... 

你也應該存儲的地方today對象的重要價值(Ex.day,月,年等),也同樣應該清除它。之後,你可以把這些數值放回去,最後你可以用它來比較medStartDate,它可以避免你因爲時間微小的不同而出現的問題。

+0

謝謝Hiral!我修改了代碼以清除醫生開始日期,現在看來按預期工作。我很驚訝它的表現如何怪異!但現在一切都OK :) – user1073326

+0

這是因爲它也計入微小的時間差異成其長時間的毫秒值。在設置所需的值之前清除這兩個日曆對象並對它們進行比較,會將默認值設置爲它們兩者,使得兩者初始相似。因此,在爲它們設置特定值後,它們將實際比較它們。 – Hiral

+0

@手記plz幫我找出兩個日期之間的差異我計算,但顯示一些變化的實際天.... plz ... –