How to format Milliseconds [duplicate] - java

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);

Related

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

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

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();

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

Time conversion [duplicate]

This question already has answers here:
Java: Converting an input of seconds into hours/minutes/seconds
(2 answers)
Closed 6 years ago.
How to convert from duration (in minutes) to hour and minutes?
For example: hour = 8 and minute = 25. Now person enter duration (in minutes) 60 for example. Now how to show time 9:25 because duration was 60 minutes?
You can use LocalTime provided in java.time.
LocalTime time = LocalTime.of(8, 25).plusMinutes(60);
System.out.println(time);
all date/time values in Java are internally in milliseconds. So multiply by the right factor to get milliseconds (60000 for minutes, 3600000 for hours). this can be added or subtracted from a date.
function addMinutes(startDate, minutes) {
var millis = startDate.getTime();
return new Date(millis + minutes*60000);
}
function addHours(startDate, hours) {
var millis = startDate.getTime();
return new Date(millis + hours*3600000);
}

The difference between two java.sql.Time objects measured in minutes [duplicate]

This question already exists:
how to calculate difference between two dates using java [duplicate]
Closed 7 years ago.
I need to know the difference between two java.sql.Time objects and the difference must be measured in minutes.
This is how I do it:
java.sql.Time srTime = sr.getTime(); // 12:40:04
java.sql.Time chTime = ch.getTime(); // 12:32:00
long diff = Math.abs(srTime.getTime() - chTime.getTime()); // 484000
The result (diff) is equal to 484000. How to make it equal to 8 minutes?
Use division
int result = 484000
/ 1000 /* take out milliseconds */
/ 60 /* convert to minutes */;
System.out.println(result);
Output:
8
You can simply divide it like this:
long diff = Math.abs(srTime.getTime() - chTime.getTime());
long res = diff/60000;
The diff which you are getting is in milliseconds, so you need to divide it by 60000 to get that in minutes.
If you are using java 1.7 you can use the TimeUnit object.
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html
Once you are getting a response um milliseconds, look at this example
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);

Categories