This question already has answers here:
Why is this not casting to long
(1 answer)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 4 years ago.
I need to subtract several months of time from a java.util.Date object to get a different java.util.Date. I know that there is a simple way to do this in java 8, however this must be run on my school's server that does not support java 8. My current solution looks like this:
int numDaysToSubtract = 60;
Date curDate = new Date();
//subtract numdays * hours/day * mins/hour * secs/min * millisecs/sec
Date newDate = new Date(curDate.getTime() - (numDaysToSubtract * 24 * 3600 * 1000));
curDate is 4/12/2018 and the calculated newDate is 4/2/2018, which is clearly not 60 days before 4/12/2018.
Why isn't this working as expected?
What should I try instead?
You have overflowed the number of milliseconds to subtract, because the product doesn't fit in a 32-bit integer, whose maximum value is about 2.1 billion (10 digits).
60 days worth of milliseconds is 5,184,000,000, over the limit. Because of the overflow, the product is calculated at 889,032,704 milliseconds, or about 10.2 days.
Either cast numDaysToSubtract as a long, or declare it to be long to begin with, to force long calculations, avoiding overflow. For me, doing that results in February 11, 2018, 60 days ago.
Related
This question already has answers here:
WHERE datetime older than some time (eg. 15 minutes)
(2 answers)
Adding/Subtracting 5 seconds from Java Date - Showing deprected warning
(9 answers)
Changing Java Date one hour back
(11 answers)
Closed 2 years ago.
I need to delete some records from the database. In the table, I have a column with Date (java.util.Date) records. I need to get from the table the rows that are older than x hours for example. My function has an int Y that deserves seconds. I need to make something like that. Date (currentDate) - Y. And after that to make a query where I compare that column with the calculation. Any suggestion?
First, if you work with java 8 or higher I would strongly advise not to use outdated class java.util.Date and switch to package java.time. You can use class ZonedDateTime or any of its "brothers" such as LocalDateTime or others.
But, to answer your question here is how you can substruct seconds from Date class:
Date date = new Date(); //Current date
long seconds = 100L; // 100 seconds to substruct
date.setTime(date.getTime() - seconds * 1000); //convert seconds to milliseconds and substruct it from current value of date
I solved this with this function. I hope will be helpful for somebody.
private Date calculateTheDate(final int seconds){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, - seconds);
return calendar.getTime();
}
This question already has answers here:
Number of days between two dates in Joda-Time
(9 answers)
Closed 5 years ago.
Upon testing JODA time to get number of days that has been selected, it is excluding the start day.
Date from = day1.getTime();
Date to = day2.getTime();
int daysBetween = Days.daysBetween(new DateTime(from), new DateTime(to)).getDays();
So if I select, December 14 - 16, it is only showing "2" as result instead of 3.
Is it safe to just "+1" the result or is there a right way to do this in JODA time?
Actually Days.daysBetween(-,-) method just subtracts the start day from the end day. If you want to get the total no of days including the start day then you must have to minus 1 from the start date.
This question already has answers here:
Android how to get time difference between two time zones in android?
(4 answers)
Rails: Difference between timezones in hours
(1 answer)
Closed 8 years ago.
I'd like to get the number of hours between 2 different time-zones. I'm currently using Joda time but it looks like it's taking daylight savings into account because it's off by an hour. The only correct one seems to be my current timezone where it returns 0 hours but if I use London for instance which for me is 5 hr difference it returns 4.
Current code:
DateTime endTime = new DateTime(date);
DateTime startTime = new DateTime();
Period period = new Period(startTime, endTime);
int hours = period.getHours();
This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 8 years ago.
I have an Problem with calculating an Age..
public long getAlter() {
Date now = new Date();
long yearInMillis = 365 * 24 * 60 * 60 * 1000;
long alter = (now.getTime() - buildYear.getTime()) /365/24/60/60/1000 ;
return alter;
}
The buildYear is given: Year 22.05.2007.
--> (dd.mm.yyyy)
I need a function to calculate the Year (not the Month or so), in this example here: 7 Years.
I need a function to calculate the Days, in this example 2576 Days.
How can I define this?
Like this:
http://www.topster.de/kalender/zeitrechner.php?styp=zeit&sdatum=22.05.2007&szeit=12%3A00%3A00&edatum=&ezeit=12%3A00%3A00&typ=jetzt&subDazu=%2B&jahredazu=0&tagedazu=0&zeitdazu=00%3A00%3A00
(already choosen)
#ALL: It's not an duplicate, because I will not work with Joda!
This link seems to have done something similar:
How do I calculate someone's age in Java?
In case the link ever breaks, here is what is said:
Check out Joda, which simplifies date/time calculations (Joda is also
the basis of the new standard Java date/time apis, so you'll be
learning a soon-to-be-standard API).
EDIT: Java 8 will have something very similar and will be worth
checking out.
e.g.
LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
which is
as simple as you could want. The current Java stuff is (as you've
identified) somewhat unintuitive.
This question already has answers here:
Difference in days between two dates in Java?
(19 answers)
Closed 8 years ago.
I m trying to get difference in miliseconds between current time and a certain day say 2013/12/25
I am using this code
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(Calendar.MONTH,11); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2013);
Calendar today = Calendar.getInstance();
long diff = thatDay.getTimeInMillis() - today.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
but this code some times give right value some time slightly difference..please help guys
In places that observe daylight savings time one day in a year is 23 hours long, and another is 25 hours long, so it's wrong to assume that each day is 24 hours long. Official time zones can also change.
The "correct" way to fix the issue is using well designed library such as Joda time, but as a quick fix you can round the result so that deviations of a few hours to one direction or another don't matter:
long days = Math.round((double) diff / (24 * 60 * 60 * 1000);