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();
}
Related
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 4 years ago.
I am trying to get a date, time and day from open weather API of a specific location and specific day using latitude and longitude. But it gives me a long integer something like this 1525974999. How can I retrieve date time and day from this?
Using Java 8 Time API:
Instant.ofEpochSecond(1525974999) // returns: 2018-05-10T17:56:39Z
Using old Java Date:
new Date(1525974999 * 1000L) // returns: Thu May 10 13:56:39 EDT 2018
I'm in Eastern US time zone
The integer represents the amount of time it has been passed since January, 1, 1970. (Unix Time Stamp)
You can use a converter from unix time stamp or just do the math programmatically.
It's probably in seconds. Try this:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeReturnedByAPI * 1000);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
Here is the Calendar API: https://developer.android.com/reference/java/util/Calendar
Edit: You may want to use the version of getInstance that takes a time zone to get the local time.
Edit 2: Updated in response to comments.
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:
How to get year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?
(10 answers)
Closed 6 years ago.
I am calculating timestamp from system time as
System.currentTimeMillis()
in java.
Later, i want to calculate year , moth and date from this time stamp.
is there any way to do so?
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
int year = cal.get(Calendar.YEAR);
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 to create a Date object, using UTC, at a specific time in the past?
(2 answers)
Closed 5 years ago.
How can I return a Date object of 4 hours less than the current system time in Java?
If you're already on Java 8 or newer:
LocalDateTime fourHoursAgo = LocalDateTime.now().minusHours(4);
Or if you want to take DST (Daylight Saving Time) into account (just in case it coincidentally went into or out DST somewhere the last 4 hours):
ZonedDateTime fourHoursAgo = ZonedDateTime.now().minusHours(4);
Or if you're not on Java 8 yet:
Date fourHoursAgo = new Date(System.currentTimeMillis() - (4 * 60 * 60 * 1000));
And you want to take DST into account:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
Date fourHoursAgo = calendar.getTime();
The other answers are correct, but I would like to contribute the modern answer. The modern solution uses java.time, the modern Java date and time API.
Instant fourHoursAgo = Instant.now().minus(Duration.ofHours(4));
System.out.println(fourHoursAgo);
This just printed:
2018-01-31T15:22:21.113710Z
The Z in the end indicates that the time is printed in UTC — at UTC offset zero if you will. The Instant class is the modern replacement for Date, so I recommend you stick to it. The modern API is generally so much nicer to work with, so much cleaner, so much better designed.
Please note the advantages of letting the library class do the subtraction of 4 hours for you: the code is clearer to read and less error-prone. No funny constants, and no readers taking time to check if they are correct.
If you do need an old-fashioned Date object, for example when using a legacy API that you cannot change or don’t want to change, convert like this:
Date oldfashionedDate = Date.from(fourHoursAgo);
Link: Oracle Tutorial trail: Date Time. Of course there are other resources on the internet too, please search.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
calendar.getTime();
Convert it to milliseconds, subtract the number of milliseconds in 4 hours, convert it back to a Date.
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, -4);
java.util.Date d = c.getTime();
System.out.println(d);
Calendar c =Calendar.getInstance() ;
c.add(Calendar.HOUR,-4);
Date d = c.getTime();
Use a Calendar object and the add method.
calendar.add(Calendar.HOUR, -4);
See http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html