Sunday, October 16, 2011

[android-developers] Time/CountDownTimer: Wrong millis returned for date/time - HELP PLEASE!!

I am writing a program for my kids to count down to Christmas. I have
been playing with android programming for a year, read books, look
online for help, but this one is really got me stumped.

In the java, I use Time (var) .set(mm, dd, yyyy) and long millis =
(var) .toMillis(true) to convert 12/25/2011 to milliseconds from the
epoch. According to http://www.esqsoft.com/javascript_examples/date-to-epoch.htm,
12/25/2011 should equal 1324789200. But when I look at the converted
result in the test window (I added code to show the "long" results
before the human readable) it shows 12/25/2011 as -877432832.

My code is below. I'm sure that I'm missing something simple/stupid.
Any help is greatly appreciated!!

Best, -Larry

### CODE BELOW ###


package com.mavrick.countdowntester3;

import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.Time;
import android.widget.TextView;

public class counttime extends Activity {
private TextView mTextField;
private Time TimerSet;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView mTextField = (TextView)
findViewById(R.id.textView1);

Time TimerSet = new Time();
TimerSet.set(12, 25, 2011); // set the date to Dec 25, 2011,
12am
TimerSet.normalize(true);
long millis = TimerSet.toMillis(true);

Time TimeNow = new Time();
TimeNow.setToNow(); // set the date to Current Time
TimeNow.normalize(true);
long millis2 = TimeNow.toMillis(true);

long millisset = millis - millis2; //subtract current from
future to set the time remaining

final int smillis = (int) (millis); //convert long to integer to
display conversion results
final int smillis2 = (int) (millis2);

new CountDownTimer(millisset, 1000) {
public void onTick(long millisUntilFinished) {

// decompose difference into days, hours, minutes and
seconds parts
int weeks = (int) ((millisUntilFinished / 1000) /
604800);
int days = (int) ((millisUntilFinished / 1000) / 86400);
int hours = (int) (((millisUntilFinished / 1000) - (days
* 86400)) / 3600);
int minutes = (int) (((millisUntilFinished / 1000) - ((days
* 86400) + (hours * 3600))) / 60);
int seconds = (int) ((millisUntilFinished / 1000) % 60);
int millicn = (int) (millisUntilFinished / 1000);

//write the data: future time, current time, remaining
time, then human readable

mTextField.setText(smillis + " " + smillis2 + " " +
millicn + "Time remaining: " + weeks + " " + days + " " + hours + " "
+ minutes + " " + seconds);
}

public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate