0

I wolud like to know the difference in milliseconds within two date and time, I use the code below but it add 1 hour more is the TimeZone but on my phone is set to automatically timezone.... :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tempo_text = (TextView)findViewById(R.id.tempo_textv);
    Date data = new Date();
    long oggi = data.getTime();
    long finish = Delay(12,30, 18, 04, 2012);
    new TempoIndietro(finish-oggi,1000).start();
}

public class TempoIndietro extends CountDownTimer{

    public TempoIndietro(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);   
    }

    @Override
    public void onFinish() {
    }

    @Override
    public void onTick(long millisUntilFinished) {
        SimpleDateFormat df = new SimpleDateFormat("dd : HH: mm: ss"); 
        df.setTimeZone(TimeZone.getDefault()); 
        String risultato = df.format(new Date(millisUntilFinished)); //arg0 tempo in ms preso dall'onTick
        tempo_text.setText(risultato);

    };

}

public long Delay(int mHour, int mMinute, int giorno, int mese, int anno){
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault());
    cal.set(Calendar.DAY_OF_MONTH, giorno);
    cal.set(Calendar.MONTH, mese);
    cal.set(Calendar.YEAR, anno);
    cal.set(Calendar.HOUR_OF_DAY, mHour);
    cal.set(Calendar.MINUTE, mMinute);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    long delay = cal.getTimeInMillis();
    return delay;

}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
MimmoG
  • 631
  • 3
  • 14
  • 25

2 Answers2

0

Take Help From this Question differece between 2 datetime

It will convert in days you just get difference between two datetime and store it in long variable.

Community
  • 1
  • 1
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • Ok but then I want show a countdown to this difference in this format days : houres : minutes : secondes how can I do ? – MimmoG Apr 17 '12 at 12:15
0

I would use the following approach assuming date2 > date 1:

  1. use the Date.getTime() method on date1 and date2
  2. subtract the two values
  3. Create a calendar object and set the time in millis to the new value
Calendar c = Calendar.getInstance();
c.setTimeInMillis(timeDifference);
  1. Get the values using get(Calendar.DAY_OF_MONTH) get(Calendar.HOUR) etc.

If your difference is greater than 31 days then you'd have to factor in the month and perhaps year calculations.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265