Found 33676 Articles for Programming

Java program to get the number of minutes in this duration

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

353 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

173 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

276 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

574 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

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

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

824 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 convert LocalDate to java.util.Date in UTC

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

5K+ 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

208 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

Java Program to find keys from a Linked HashMap and store it in a list

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

177 Views

Let us first create a LinkedHashMap with key-value pair −Mapmap = new LinkedHashMap(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); map.put("5", "Colin"); map.put("6", "Andre"); map.put("7", "Pollard"); map.put("8", "David"); map.put("9", "Jofra"); map.put("10", "Akila");Now, create a new List and store the keys in it for the above Map −Listlist = new ArrayList(); list.addAll(map.keySet());Example Live Demoimport java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("1", "Katie");       map.put("2", "Peter");       map.put("3", "Amy");       map.put("4", "Kane");     ... Read More

Java Program to find keys from both the Linked HashMap and store it in a list alternatively

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

119 Views

Let us first create a LinkedHashMap with key-value pair −Mapmap1 = new LinkedHashMap(); map1.put("1", "Jim"); map1.put("2", "David"); map1.put("3", "Tom"); map1.put("4", "Sam"); map1.put("5", "Steve");Let us now create another LinkedHashMap with key-value pair −Mapmap2 = new LinkedHashMap(); map2.put("6", "Katie"); map2.put("7", "John"); map2.put("8", "Kane"); map2.put("9", "Chris");Now, create a new List and store the keys in it for both the above Map −Listlist = new ArrayList(); list.addAll(map1.keySet()); list.addAll(map2.keySet());Example Live Demoimport java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap1 = new LinkedHashMap();       map1.put("1", "Jim");       map1.put("2", "David"); ... Read More

Advertisements