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();
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:
How to calculate the number of days in a period?
(3 answers)
Closed 3 years ago.
I have Period object which comes from api. I have to calculate total days it contains. I found many answers how to get days between two dates, but no one answers how I can get total days exactly from Period object.
E.g:
LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusYears(1);
Period period = Period.between(start, end);
I have only the last object with name period and i have to get 365 days from it.
getDays() returns only days count within one month. And I don't have two dates objects. Only period.
You might wanna read up on the Period Java API Documentation.
Period represents the time duration in this format, "2 years, 3 months and 4 days"
For your case above, its exactly 1 year, 0 Months and 0 days.
When you do a get Days, it will show you 0 days.
Try with an end date of LocalDate end = LocalDate.now().minusDays(398);
The getYears(), getMonths(), getDays() each returns its own value. ;)
Since you already have a Period object, you can do a function to get the number of days from a Period Object using the getYears, getMonths and getDays() methods then sum them together.
The Duration object might help with that.
did you tried import java.time package and use Period class? if you didn't, here's a little help for it https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
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:
convert joda-time seconds to formatted time string
(3 answers)
Closed 7 years ago.
I am using JodaTime for time management in my app.
I want to get the hour and minute from a DateTime with 2 digits.
Example:
if the hour is 8 and the minutes 5
I want to get 08:05
DateTime dt = DateTime.now();
DateTimeFormatter dtf = DateTimeFormat.forPatter("H:m");
dt.toString(dtf);
I know that I can achieve this with manual string concat, but I don't want it.
have you tried "HH:mm" for pattern? it will print Hours and minutes using 2 digits for each (in your case 08:05)