[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, ) 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Time/CountDownTimer: Wrong millis returned for date/time - HELP PLEASE!!
Possibly a signed/unsigned problem. Try using an unsigned long. On 10/17/2011 4:55 AM, Larry/MavrickProductions wrote: 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, ) 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Time/CountDownTimer: Wrong millis returned for date/time - HELP PLEASE!!
The value from the JavaScript is in seconds. Converted to milliseconds, it does not fit in a 4-byte integer (0x*134*_7396F480_), that's why normally longs (8 bytes) are used for time stamps. You don't really need to convert the values to ints just for the sake of a number-to-string conversion. Either declare smillis, smillis2 as longs and remove the typecast (to see milliseconds): final*long*smillis = millis; final*long*smillis2 = millis2; or divide millis and millis2 by 1000 (to see seconds): final int smillis = (int) (millis* / 1000L*); final int smillis2 = (int) (millis2* / 1000L*); -- Kostya 17.10.2011 0:55, Larry/MavrickProductions ?: 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, ) 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(); } } -- Kostya Vasilyev -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en