Java equivalent syntax for time difference code in C# [duplicate] - java

This question already has answers here:
Calculating time difference in Milliseconds
(9 answers)
Closed 4 years ago.
How can be write below c# code in java?
var startTime = DateTime.Now;
//some code
Duration = DateTime.Now.Subtract(startTime).TotalMilliseconds;
value in Duration variable is float.

Pretty the same.
Date startTime = new Date();
//some code
long duration = new Date().getTime() - startTime.getTime();

Related

How to subtract dates using java.sql.Timestamp [duplicate]

This question already has answers here:
How do I measure time elapsed in Java? [duplicate]
(15 answers)
In Java, how do I get the difference in seconds between 2 dates?
(13 answers)
How do I write a correct micro-benchmark in Java?
(11 answers)
Closed 2 years ago.
I am practically checking how much time taking by collection(s) insertion with 'N' elements,
Now I am stuck in checking total time taken by ArrayList in Insertion process.
Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
System.out.println("Start Insertion :: "+startTimeStamp);
List<Integer> intList = new ArrayList<>();
for (int i = 0; i <= 100000000; i++) {
intList.add(i);
}
Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
System.out.println("End insertion :: "+endTimeStamp);
// Total time taken
// TODO
Output :
Start Insertion :: 2020-03-19 16:47:27.395
End insertion :: 2020-03-19 16:48:11.963
The simple, old-school way would be to use the getTime() method on each Timestamp and subtract the results, giving you the number of milliseconds elapsed between the two:
long millisElapsed = endTimeStamp.getTime() - startTimeStamp.getTime();
Using more modern APIs, though, you would probably convert each timestamp to an Instant, and compute the Duration bracketed by those:
Duration elapsedDuration =
Duration.between(startTimeStamp.toInstant(), endTimeStamp.toInstant());
A Duration has considerably more structure and support than a primitive long, but perhaps it's more than you need.

Question regarding output shown after converting nanoseconds to seconds [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 3 years ago.
I'm having trouble understanding the output generated by the code below.
The code segment:
long startTime= System.nanoTime();
//method that I am measuring.
long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;
double seconds = (double)elapsedTime / 1_000_000_000.0;
System.out.println(seconds+"secs");
One of the outputs that I got:
1.397E-4 secs
1.397E-4 is the same as 0.0001397

How to calculate the time difference in seconds? [duplicate]

This question already has answers here:
Java 8: Calculate difference between two ZonedDateTime
(3 answers)
Closed 5 years ago.
I have two Datetime strings:
val formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd-hh-mm-ss")
val startTime = formatter format ZonedDateTime.now(ZoneId.of("UTC"))
//...
val endTime = formatter format ZonedDateTime.now(ZoneId.of("UTC"))
How can I calculate the difference in seconds between endTime and startTime?
Use a java.time.temporal.ChronoUnit:
// difference in seconds
ChronoUnit.SECONDS.between(startTime, endTime)
But startTime and endTime must be ZonedDateTime objects, not Strings.
Just keep in mind that the result is floor-rounded - if the real difference is, let's say, 1999 milliseconds, the code above will return 1 (because 1999 milliseconds are not enough to make 2 seconds).
Another detail is that you can use ZoneOffset.UTC instead of ZoneId.of("UTC"), as the result is the same.
Actually, if you're working with UTC, why not use Instant.now() instead? The between method above works the same way with Instant's:
val start = Instant.now()
val end = Instant.now()
val diffInSecs = ChronoUnit.SECONDS.between(start, end)

Find the difference between two dates (Inclusive of start and end date) in Java [duplicate]

This question already has answers here:
Calculating difference in dates in Java
(7 answers)
Closed 5 years ago.
I need to find the difference between two dates in Java and the difference should be inclusive of start and end date. I tried using below piece of code but it is not including start and end date.
long diffDays = Days.daysBetween(new DateTime(startDate), new DateTime(endDate)).getDays();
Is there any utility method to achieve this?
If you're not using a library this would be one of the method:
public static Map<TimeUnit,Long> computeDiff(Date date1, Date date2) {
long diffInMillies = date2.getTime() - date1.getTime();
List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
Collections.reverse(units);
Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
long milliesRest = diffInMillies;
for ( TimeUnit unit : units ) {
long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS);
long diffInMilliesForUnit = unit.toMillis(diff);
milliesRest = milliesRest - diffInMilliesForUnit;
result.put(unit,diff);
}
return result;
}
Or else you could use Joda

How to format Milliseconds [duplicate]

This question already has answers here:
How do I format a number in Java?
(9 answers)
Closed 6 years ago.
In my code I'm getting milliseconds using
// elaspsed time in milliseconds
public long getElapsedTime() {
if (running) {
return System.currentTimeMillis() - startTime;
}
return stopTime - startTime;
}
As long is a whole number Where as I want to get the milliseconds in 00.0000 format
However I've tried this so far
return ((System.currentTimeMillis() - startTime) / 1000f);
but I am not getting to particular format
It is not possible for you to set the return type to long and return a double or String value.
However if you need to format a millisecond value to the provided format, you should be able to get it done using following code snippet.
String.format("%d.%d", TimeUnit.MILLISECONDS.toSeconds(millies),millies%1000);

Categories