In Java, How to Get the Difference in Seconds Between 2 Dates

In Java, how do I get the difference in seconds between 2 dates?

Not familiar with DateTime...

If you have two Dates you can call getTime on them to get millseconds, get the diff and divide by 1000. For example

Date d1 = ...;
Date d2 = ...;
long seconds = (d2.getTime()-d1.getTime())/1000;

If you have Calendar objects you can call

c.getTimeInMillis()

and do the same

Calculate date/time difference in java

try

long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);

NOTE: this assumes that diff is non-negative.

Get difference of seconds beetween two dateTime

int seconds = (int) ChronoUnit.SECONDS.between(now, midnight); 

Calculating the difference between two Java date instances

The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library.

Joda Time has a concept of time Interval:

Interval interval = new Interval(oldTime, new Instant());

EDIT: By the way, Joda has two concepts: Interval for representing an interval of time between two time instants (represent time between 8am and 10am), and a Duration that represents a length of time without the actual time boundaries (e.g. represent two hours!)

If you only care about time comparisions, most Date implementations (including the JDK one) implements Comparable interface which allows you to use the Comparable.compareTo()

How to find the duration of difference between two dates in java?

try the following

{
Date dt2 = new DateAndTime().getCurrentDateTime();

long diff = dt2.getTime() - dt1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
int diffInDays = (int) ((dt2.getTime() - dt1.getTime()) / (1000 * 60 * 60 * 24));

if (diffInDays > 1) {
System.err.println("Difference in number of days (2) : " + diffInDays);
return false;
} else if (diffHours > 24) {

System.err.println(">24");
return false;
} else if ((diffHours == 24) && (diffMinutes >= 1)) {
System.err.println("minutes");
return false;
}
return true;
}

How can I calculate a time difference in Java?

String time1 = "16:00:00";
String time2 = "19:00:00";

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();

Difference is in milliseconds.

I modified sfaizs post.

How to get the time difference in seconds?

LocalDate is just that, date only, no time information. Use LocalDateTime instead

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Seconds;

public class Test {

public static void main(String[] args) {

DateTime today = DateTime.now();
today = today.minusDays(5);
int dateDiff = getDateDifference(today);
System.out.println("Timestamp: " + dateDiff);
}

public static int getDateDifference(DateTime dateCreatedPa) {
LocalDateTime dateCreated = new LocalDateTime(dateCreatedPa);
LocalDateTime now = new LocalDateTime();

System.out.println(dateCreated);
System.out.println(now);

Seconds secondsBetween = Seconds.secondsBetween(dateCreated, now);

return secondsBetween.getSeconds();
}

}

Outputs...

2015-02-27T15:20:56.524
2015-03-04T15:20:56.628
Timestamp: 432000

Java get the number of seconds between 2 dates

If you're "trying to send data every 10 seconds", you can just do

ScheduledExecutorService service = Executors.newScheduledThreadPool();

service.scheduleAtFixedRate(() -> sendUdp("OK"), 10, 10, TimeUnit.SECONDS);

difference in seconds between two dates using joda time?

Use the Seconds class:

DateTime now = DateTime.now();
DateTime dateTime = now.plusMinutes(10);
Seconds seconds = Seconds.secondsBetween(now, dateTime);
System.out.println(seconds.getSeconds());

This piece of code prints out 600. I think this is what you need.

As further advice, explore the documentation of joda-time. It's pretty good, and most things are very easy to discover.

In case you need some help with the parsing of dates (It's in the docs, really), check out the related questions, like this:

Parsing date with Joda with time zone



Related Topics



Leave a reply



Submit