Found 7442 Articles for Java

How to get the duration between two Instant timestamps in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

21K+ Views

Let us first set two Instants with ofEpochSeconds() method using seconds from the epoch of 1970-01- 01T00:00:00Z.Now get the duration between the above two Instant:Duration res = Duration.between(one, two);Exampleimport java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant one = Instant.ofEpochSecond(1845836728);       Instant two = Instant.ofEpochSecond(1845866935);       Duration res = Duration.between(one, two);       System.out.println(res);    } }OutputPT8H23M27S

Java Program to get Milli seconds from Instant

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

243 Views

Create an Instant and get an Instant using milliseconds passed as parameter from the epoch of 1970-01- 01T00:00:00Z. This is done using ofEpochMilli():Instant instant = Instant.ofEpochMilli(342627282920l);Now, get the Epoch milliseconds from Instant:instant. toEpochMilli();Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(1262347200000l);       long res = instant.toEpochMilli();       System.out.println(res);    } }Output1262347200000

Java Program to get Epoch seconds from Instant

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

245 Views

Create an Instant and get an Instant using milliseconds passed as parameter from the epoch of 1970-01- 01T00:00:00Z. This is done using ofEpochMilli():Instant instant = Instant.ofEpochMilli(342627282920l);Now, get the Epoch seconds from Instant:instant.getEpochSecond();Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(342627282920l);       long res = instant.getEpochSecond();       System.out.println(res);    } }Output342627282

Java Program to create Instant from Epoch second and millisecond

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

Create Instant from Epoch SecondExampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochSecond(282829279);       System.out.println(instant);    } }Output1978-12-18T11:41:19ZCreate Instant from Epoch MillisecondsExampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(272827282728l);       System.out.println(instant);    } }Output1978-08-24T17:21:22.728Z

Java Program to convert this duration to the total length in nanoseconds

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

177 Views

With this, get the nanoseconds in days, hours and minutes. At first, set the Duration:Duration d1 = Duration.ofDays(5); Duration d2 = Duration.ofHours(20); Duration d3 = Duration.ofMinutes(15);Convert the above Duration to nanoseconds:System.out.println("Nanoseconds in 5 days = "+d1.toNanos()); System.out.println("Nanoseconds in 20 hours = "+d2.toNanos()); System.out.println("Nanoseconds in 15 minutes = "+d3.toNanos());Exampleimport java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration d1 = Duration.ofDays(5);       Duration d2 = Duration.ofHours(20);       Duration d3 = Duration.ofMinutes(15);       System.out.println("Nanoseconds in 5 days = "+d1.toNanos());       System.out.println("Nanoseconds in 20 hours = "+d2.toNanos());   ... Read More

Java program to get the number of minutes in this duration

Krantik Chavan
Updated on 18-Nov-2024 22:30:42

358 Views

In this article, we calculate the number of minutes in a given duration using Java. The Duration class can perform time-based calculations like hours, minutes, days, etc.. and easily convert between these units. Using the toMinutes() method you will be able to get the number of minutes from a given Duration. Problem StatementGiven a Duration object representing a period of time, write a Java program to calculate the number of minutes in the specified duration.Input Duration = 25 days, 10 hoursOutput Minutes in 25 days = 36000Minutes in 10 hours = 600 Steps to get the number of ... Read More

Java program to create duration from seconds

Krantik Chavan
Updated on 15-Nov-2024 18:43:20

175 Views

In this article, we will learn how to create a Duration object in Java based on different time units like days, hours, milliseconds, and minutes, and then convert them into seconds. The Duration class in Java is part of the java.time package and is used to represent a duration of time in a standardized way. Problem StatementGiven time durations in days, hours, milliseconds, and minutes, write a Java program to create Duration objects from these values and calculate how many seconds they represent.Input Duration in days = 10Duration in hours = 10Duration in milliseconds = 10Duration in minutes = ... Read More

Java Program to remove an element from List with ListIterator

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

279 Views

Let’s say the following is our List with elements −ArrayList < String > arrList = new ArrayList < String > (); arrList.add("Jack"); arrList.add("Tom"); arrList.add("Brad"); arrList.add("Amy"); arrList.add("Ben"); arrList.add("Peter"); arrList.add("Katie"); arrList.add("Tim");Now, use the listIterator(). The next() method returns the next element in the List. Hoverer, remove an element using remove() method −ListIteratoriterator = arrList.listIterator(); iterator.next(); iterator.remove();Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add("Jack");       arrList.add("Tom");       arrList.add("Brad");       arrList.add("Amy");       arrList.add("Ben");       arrList.add("Peter"); ... Read More

Java Program to sort a List in case insensitive order

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

Let’s say your list is having the following elements −P, W, g, K, H, t, ETherefore, case sensitive order means, capital and small letters will be considered irrespective of case. The output would be −E, g, H, K, P, t, WThe following is our array −String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };Convert the above array to a List −Listlist = Arrays.asList(arr);Now sort the above list in case insensitive order −Collections.sort(list, String.CASE_INSENSITIVE_ORDER);Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] argv) throws Exception {       ... Read More

Java program to merge duplicates of a List with TreeSet

karthikeya Boyini
Updated on 30-Oct-2024 18:36:14

578 Views

In this article, we will learn to merge duplicate elements from a List in Java by using a TreeSet. This program will take a List of strings containing duplicate values, transfer the elements into a TreeSet, and display the unique, sorted elements. Using a TreeSet is ideal here because it automatically removes duplicates and orders the elements in natural order. Problem Statement Write a Java program to remove duplicate elements from a list using a TreeSet. The program should take a list with repeated elements, convert it into a TreeSet to eliminate duplicates, and display the unique, sorted elements. Input ... Read More

Advertisements