I want to use milliseconds to set a new date in my program,but it doesnt work. Is there anybody can tell me why it doesnt work?
Calendar r_1 = new GregorianCalendar(2011,0,1);
r_1.add(Calendar.DAY_OF_MONTH,2);
System.out.println(r_1.getTime());
long date_1 = r_1.getTimeInMillis() + 2*24*60*60*1000;
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
It works both very correct , but if i change the day from 2 to 25,then it doenst work .
----------the output is correct ,it is 2011/01/26 ----------
Calendar r_1 = new GregorianCalendar(2011,0,1);
r_1.add(Calendar.DAY_OF_MONTH,25);
System.out.println(r_1.getTime());
-----------the output is incorrect now ,it is 2010/12/07------
long date_1 = r_1.getTimeInMillis() + 25*24*60*60*1000;//i have change 2 to 25
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
Thanks
The expression 25*24*60*60*1000 is an integer, and you have overflowed the size of an integer, creating a negative number.
Your expression is 2,160,000,000 milliseconds. The largest value an int can hold is 2,147,483,647.
To fix this, you have to force the expression to be a long, as follows
25L*24*60*60*1000
25*24*60*60*1000 is too large to fit in an int.
Try 25L*24*60*60*1000 which is a long constant.
Try something like that:
final long k = 25*24*60*60*1000L;
long date_1 = r_1.getTimeInMillis() + k;
Related
How can I get an Integer to be two digits. If it's under 10 it would show 01,02, etc. I want to keep the Integer as an Integer. String.format gives a string. How can I do this with Decimalformat for ex
Decimalformat df = new Decimalformat(???);
If you want to keep the value as an int or Integer, there's nothing to be done here. A number doesn't have a format - it just has a value. For example, an int isn't "in decimal" or "in hex" or "in binary" - if you write
int x = 16;
int y = 0x10;
those are the exact same values.
The idea of "padding" only makes any sense when it comes to a textual representation of the number - which is when you end up with a string.
you do something likewise, but directly you can't get into int or Integer
NumberFormat formatter = new DecimalFormat("00");
String s = formatter.format(1); // ----> 01
I have a countdown timer which shows seconds from 60 to 0 (1 min countdown timer). When it reaches 1 digit numbers such as 9,8,7.. it shows 9 instead of 09. I tried using String.format("%[B]02d[/B]", x); where I converted x from long to string. It didn't work.
I want an equivalent of String.format("%2d", 1)
You can accomplish it with DecimalFormat:
NumberFormat f = new DecimalFormat("00");
long time = 9;
textView.setText(f.format(time));
Output:
09
Or you can use String.format() as well:
String format = "%1$02d"; // two digits
textView.setText(String.format(format, time));
Use: text.setText(String.format("%02d", i)); where i is the integer value
Why not just use an if statement?
String str = x < 10 ? "0" + String.valueOf(x) : String.valueOf(x);
That should do the trick.
TextView time;
int hour=0,minute=0,second=0;
time.setText((String.format("%02d", hour))+":"+(String.format("%02d", minute))+":"+(String.format("%02d", second)));
time to TextView
Try using this:
tv.setText(new DecimalFormat("##").format(var));
I'm very confused right now.
I have a GregorianCalendar object which I give a specific date (Jan 1st 2010).
Calendar:
Calendar c = new GregorianCalendar();
c.set(2010, 0, 1);
System.out.println(c.getTime());
System.out.println(c.getTimeInMillis());
Output:
Fri Jan 01 13:12:57 CET 2010
1262347977927
Now when I try to create a long and store this number in it, the number is actually too big for my variable.
Storing in variable:
long timeStamp = 1262347977927;
// ERROR: The literal 1262347977927 of type int is out of range
But when I store the result directly into my variable, it works just fine.
Directly storing:
long timeStamp = c.getTimeInMillis();
System.out.println(timeStamp);
Output:
1262348451631
Why is the long that I get too big to be a long, yet not too long to be a long? I'm confused.
I'm using Java 6 and Eclipse Indigo if anyone would want to know.
EDIT: Thanks for all the instant answers... I feel really stupid now :p
You just have to add a L after the literal:
long timeStamp = 1262347977927L;
Always refer to the docs, Primitive Data Types:
An integer literal is of type long if it ends with the letter L or l;
otherwise it is of type int. It is recommended that you use the upper
case letter L because the lower case letter l is hard to distinguish
from the digit 1.
User the L at the end of the number:
long timeStamp = 1262347977927L;
It defines the Number as Long.
You must append an L or l
long timeStamp = 1262347977927L;
literal to the assigned value otherwise an integer value is assumed by the compiler
The former is preferable as the latter looks like a 1!
No 1262347977927 is not big enough for a long number (which is 8 bytes).
Make it like this:
long timeStamp = 1262347977927L;
L for long declaration.
Reason is that in Java by default all numbers are treated as int type unless you put switches like L in the end.
Or simple:
long timeStamp = c.getTimeInMillis()
Change
long timeStamp = 1262347977927;
to
long timeStamp = 1262347977927L;
Note that you need to suffix 'L' (preferred) or 'l' (suffixing this can lead to difficult to read code; there's a a java puzzler entry related to this) at the end .
It's not telling you that 1262347977927 can't fit into a long, it's telling you that it can't construct an int constant of value 1262347977927, before it even tries to assign it into the long variable. Notice how timeStamp is never mentioned in the error:
The literal 1262347977927 of type int is out of range
Use L after the number to mark it as a long constant, then you can assign it without problems:
long timeStamp = 1262347977927L;
Try appending 'L' after your number while assigning it to long variable.
I am trying to take have a time that counts down all the way to 0 from 1 minute.
I have already created my timer. I just need to know how do i convert a int such as 100 to a String to show 1:00 as in a minute and keep counting down like minus 1 secodn from the int and convert it to the String :59?
Any suggestions?
If I understand your question correctly you simply want to divide your number by 60 to get the minutes and then mod your number by 60 to get the seconds and then concat the strings.
int remainingTime = 100;// or whatever number of seconds you have left
String min = (remainingTime / 60) + "";
String sec = (remainingTime % 60) + "";
String remainingTimeStamp = "min" + ":" + "sec";
If you want to get fancy with it, check to see if min and sec are less than 10 and append a leading zero, so that it looks like 01:05 rather than 1:5
I'm not sure what you mean by "I already have my timer". I'll wait for you to update your question, rather than speculate as to what you might mean, before I continue elaborating.
To add to this, check the http://developer.android.com/reference/android/text/format/DateUtils.html class. Note that this is not in 'org.apache' which has another DateUtils class.
Edit:
Since it appears you were looking for formatting 100 seconds here is it formatted:
SimpleDateFormat df = new SimpleDateFormat("mm:ss");
String formatted = df.format(remainingTime * 1000);
I had a bug that caused an integer overflow, resulting in wrong (negative) timestamps being written to the database. The code is fixed already, but I want to fix the wrong data, too.
I thought, I could just take the wrong results and add Integer.MAX_VALUE, but that didn't seem to work, it left me with to high values. I have the offset value in the code snippet below, but the input values are not stored.
The following code reproduces the bug:
#Test
public void testArexxConversion()
{
// The input values represent seconds since midnight, Jan 1, 2000 UTC
final int sample = 361450072; // A sample input value drawn from production
// I use the offset from the UNIX epoch to convert the vakue to UNIX seconds
final int offset = 946684800; // midnight, Jan 01 2000 UTC in UNIX seconds
// This was the buggy line in my code, the assertion will fail
long result = (sample + offset) * 1000;
// Prints 'Result is negative: -1830153280'
Assert.assertTrue(result > 0, String.format("Result is negative: %d", result));
// This is for comparison
Date dt = new Date(offset * 1000);
Assert.assertEquals(dt.getTime() + sample * 1000, result);
}
How to fix the bug in your database
To fix the bug in your database you can do the following addition to all the buggy data:
long new_result = old_buggy_result + 1309965025280L;
The constant number was found like this:
Check the buggy result value
Find what should the correct result value be?
Do an addition to the buggy result value to find the correct `result.
But this is only possible if you have saved sample and offset in your database or somewhere else.
Otherwise, it depends on the number of wraps that occured during the original calculation:
long size_of_int = (long)Math.pow(2, 32);
int number_of_wraps = 305 // Only correct in your example!
// You can't deduct the number of wraps from
// the wrong value alone, because that information
// is lost in the modulo (the "wrap")
long correct_number = wrong_number + size_of_int * number_of_wraps;
If the numbers in your database are close enough to your sample value, this means, you can do the above, using 305 as the number of wraps.
Explanation of the bug (for future readers)
The operation here:
(sample + offset) * 1000;
is computed using int and not long. But the result is "too big" to be saved on an int variable. That's why you have an overflow.
Change it to:
((long) sample + offset) * 1000L;
So now the + and * operations will be done using long values, and the result will be a long value which won't overflow.
That would be like this:
long result = ... ; // bad negative from database
long new_result = (long)((int)result - Integer.MAX_VALUE) + Integer.MAX_VALUE;
Replace this line.
long result = (long)(sample + offset) * 1000L;