Found 4337 Articles for Java 8

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

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

113 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 30-Jul-2019 22:30:25

177 Views

At first, set the Duration:Duration duration = Duration.ofHours(10);Now, get the number of minutes from the above Duration that has 10 hours:d1.toMinutes()Exampleimport java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration d1 = Duration.ofDays(25);       Duration d2 = Duration.ofHours(10);       System.out.println("Minutes in 25 days = "+d1.toMinutes());       System.out.println("Minutes in 10 hours = "+d2.toMinutes());    } }OutputMinutes in 25 days = 36000 Minutes in 10 hours = 600

Java Program to create Duration from seconds

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

92 Views

We will get seconds here based on days, hours, millis and minutes, for example, how many seconds in 10 days, seconds in 10 hours, etc.Duration for days, hours, milliseconds and minutes:Duration duration = Duration.ofDays(10); Duration duration1 = Duration.ofHours(10); Duration duration2 = Duration.ofMillis(10); Duration duration3 = Duration.ofMinutes(10);Now, get the seconds:System.out.println("Seconds in 10 days = "+duration.getSeconds()); System.out.println("Seconds in 10 hours = "+duration1.getSeconds()); System.out.println("Seconds in 10 milliseconds = "+duration2.getSeconds()); System.out.println("Seconds in 10 minutes = "+duration3.getSeconds());Exampleimport java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration duration = Duration.ofDays(10);       Duration duration1 = Duration.ofHours(10);     ... Read More

Java Program to remove an element from List with ListIterator

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

208 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

2K+ 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-Jul-2019 22:30:25

368 Views

Let’s say the following is a List. In this, we have duplicates elements as well −Listls = new ArrayList(); ls.add("A"); ls.add("B"); ls.add("C"); ls.add("D"); ls.add("D"); ls.add("E"); ls.add("F"); ls.add("G"); ls.add("E");Now, create a TreeSet and merge the duplicates of the List −TreeSetset = new TreeSet(); set.addAll(ls);Example Live Demoimport java.util.ArrayList; import java.util.TreeSet; import java.util.List; public class Demo {    public static void main(String[] args) {       Listls = new ArrayList();       ls.add("A");       ls.add("B");       ls.add("C");       ls.add("D");       ls.add("D");       ls.add("E");       ls.add("F");       ls.add("G");   ... Read More

How to fill multiple copies of specified Object to a List in Java

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

660 Views

To fill multiple copies of specified object to a list means let’s say you have an element 100 and want to display it 10 times. For this, let us see an example.The following is our list and iterator. We have used nCopiec Collections method to set the elements and how many copies you want −Listlist = Collections.nCopies(10, 100); Iteratoriterator = list.iterator();After that display the multiple copies −while (iterator.hasNext()) System.out.println(iterator.next());Example Live Demoimport java.util.Collections; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = Collections.nCopies(10, 100);       Iteratoriterator = list.iterator();   ... Read More

Java Program to sort a List in case sensitive order

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

592 Views

Let’s say your list is having the following elements −P, W, g, K, H, t, ETherefore, case sensitive order means, at first the sorted order would be in capital letters and then small −E, H, K, P, W, g, tThe 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 sensitive order −Collections.sort(list);Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] arg v) throwsException {       String[] arr = ... Read More

Java program to convert LocalDate to java.util.Date in UTC

Samual Sam
Updated on 02-Jul-2024 10:26:21

2K+ Views

In this article, we will learn how to convert a local date to a java.util.Date in UTC using Java's date and time API. Problem Statement Convert the current date, to a java.util.Date object in Coordinated Universal Time (UTC). The goal is to achieve this conversion while ensuring that the resulting java.util.Date object represents the start of the day in UTC for the given LocalDate. Steps to Convert LocalDate to java.util.Date in UTC The following are the steps to convert LocalDate to java.util.Date in UTC Step 1: Import the required classes i.e., java.time and java.util ... Read More

Java Program to convert LocalDate to java.util.Date by timezone offset

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

125 Views

Set the LocalDate to now −LocalDate date = LocalDate.now();Now, set the TimeZone offset −ZoneOffset timeZone = ZoneOffset.UTC;Convert the LocalDate to java.util.Date −Date.from(date.atStartOfDay().toInstant(timeZone))Example Live Demoimport java.time.LocalDate; import java.time.ZoneOffset; import java.util.Date; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("Date = "+date);       ZoneOffset timeZone = ZoneOffset.UTC;       System.out.println(Date.from(date.atStartOfDay().toInstant(timeZone)));    } }OutputDate = 2019-04-19 Fri Apr 19 05:30:00 IST 2019

Advertisements