+ 1
Working with Time units in JAVA
I've been trying to make a code to calculate the timespan between the hours, in C# I used the Timespan Class, it was great. But, in Java, I'm totally lost and frustrated. I reasearched on the internet, but I'd like to know what this beatiful community has to say about it. This is my C# code I'd like to write in JAVA https://code.sololearn.com/cXSyNzrfOmgV/?ref=app
2 odpowiedzi
+ 4
Additional Resources:
https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html?is-external=true
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
JAVA EXAMPLE:
https://code.sololearn.com/cy5b8HN3559T/#java
import java.time.Instant;
import java.time.Duration;
public class Program
{
public static void main(String[] args) {
Instant startTime, endTime;
startTime = Instant.now();
System.out.println(startTime);
endTime = startTime.plusSeconds(28946731);
System.out.println(endTime);
System.out.print("Days: ");
System.out.println(Duration.between(startTime,endTime).toDays());
System.out.print("Hours: ");
System.out.println(Duration.between(startTime,endTime).toHours());
System.out.print("Minutes: ");
System.out.println(Duration.between(startTime,endTime).toMinutes());
System.out.print("Seconds: ");
System.out.println(Duration.between(startTime,endTime).getSeconds());
System.out.print("Millis: ");
System.out.println(Duration.between(startTime,endTime).toMillis());
System.out.print("Nanos: ");
System.out.println(Duration.between(startTime,endTime).toNanos());
}
}
0
Thanks bro. I undestood that Jse 8 introduced this class. I'm using N-IDE to study on Android, and java.time ins't on it (I'm a total noob, i know). Before java.time it was much work to solve this problem.