Found 9150 Articles for Object Oriented Programming

Java program to get Milliseconds between two time instants

Samual Sam
Updated on 12-Aug-2024 23:13:17

3K+ Views

In this article, we will demonstrate how to calculate the difference in milliseconds between two Instant objects using the ChronoUnit class. It shows how to work with time-based classes in the java.time package, specifically focusing on creating and manipulating time instants and calculating durations between them. ChronoUnit: A standard set of date and time units lets you manipulate dates, times, and date-times. These units, such as years, months, and days, are designed to work across various calendar systems, even though the specific rules might differ slightly. You can also extend these units by implementing the TemporalUnit interface. Problem Statement Calculate the ... Read More

Java Program to convert java.util.Date to LocalDate

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

275 Views

At first set the date with java.util.Date −java.util.Date date = new Date();Now, convert the date to LocalDate −Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       Instant instant = Instant.ofEpochMilli(date.getTime());       System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());    } }OutputDate = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18

Java Program to get minimum value with Comparator

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

378 Views

First, declare an integer array and add some elements −Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };Now, convert the above array to List −List list = Arrays.asList(arr);Now, use Comparator and the reverseOrder() method. Using max() will eventually give you the maximum value, but with reverseOrder() it reverses the result −Comparator comp = Collections.reverseOrder(); System.out.println("Minimum element = "+Collections.max(list, comp));Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo {    @SuppressWarnings("unchecked")    public static void main(String args[]) {       Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };     ... Read More

Java Program to generate random elements from a given array

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

507 Views

Let’s say the following is our array −Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};Now, we can convert it to a list before shuffling it −Listlist = Arrays.asList(arr); Collections.shuffle(list);The above shuffling generates random elements. Display them like this −for (Integer res: list) {    System.out.print(res + " "); }Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};       System.out.print("Array elements...");       for (Integer res: arr) { ... Read More

Java Program to generate random number array within a range and get min and max value

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

1K+ Views

At first, create a double array −double[] val = new double[10];Now, generate and display random numbers in a loop that loops until the length of the above array. We have used nextInt here for random numbers −for (int i = 0; i < val.length; i++) {    val[i] = new Random().nextInt(100);    System.out.println(val[i]); }Now, get the min and max values. Compare each value of the random array with the MIN and MAX values −double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; for (int i = 0; i < val.length; i++) {    if (val[i] < min)       min ... Read More

Java Program to shift array elements to the right

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

470 Views

Let us first create an int array −int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };Now, shift array elements to the right with arraycopy() and placing the elements correctly so that it gets shifted to the right −System.arraycopy(arr, 0, arr, 1, arr.length - 1);Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };       System.out.println("Initial array..."+Arrays.toString(arr));       System.arraycopy(arr, 0, arr, 1, arr.length - 1);       System.out.println("Array after shifting to the right...");       System.out.println(Arrays.toString(arr));    } }OutputInitial array... [10, 20, 30, 40, 50, 60, 70, 80, 90] Array after shifting to the right... [10, 10, 20, 30, 40, 50, 60, 70, 80]

How to shuffle an array in Java?

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

3K+ Views

Declare a string array and add elements in the form of letters −String[] letters = { "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };Convert the above array to list −Listlist = Arrays.asList(letters);Now, create a shuffled array using the Random class object and generate the random letters with nextInt() −int len = list.size(); System.out.println("Shuffled array..."); for (int i = 0; i < letters.length; i++) {    int index = new Random().nextInt(len);    String shuffle = list.get(index);    System.out.println(shuffle); }Example Live Demoimport java.util.Arrays; import java.util.List; import java.util.Random; public class Demo {    public static void main(String[] args) {   ... Read More

How to randomize and shuffle array of numbers in Java?

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

735 Views

At first, create an integer array −int[] arr = { 20, 40, 60, 80, 100, 120, 140, 160, 180, 200};Now, create a Random class object −Random rand = new Random();Loop until the length of the array and shuffle the elements −for (int i = 0; i < arr.length; ++i) {    int index = rand.nextInt(arr.length - i);    int tmp = arr[arr.length - 1 - i];    arr[arr.length - 1 - i] = arr[index];    arr[index] = tmp; }Example Live Demoimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String[] args) {       int[] arr = ... Read More

Java Program to create an array with randomly shuffled numbers in a given range

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

151 Views

For randomly shuffled numbers, let us create an integer list and add some numbers −List < Integer > list = new ArrayList < Integer > (); list.add(10); list.add(50); list.add(100); list.add(150); list.add(200);Now, shuffle the elements for random values −Collections.shuffle(list);Create an int array with the same number of elements in the above list −intlen = list.size(); int[] res = newint[len];Set the shuffled values in the new array −for (inti = 0; i < len; i++) {    res[i] = list.get(i); }Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {     ... Read More

Java Program to get maximum value with Comparator

Alshifa Hasnain
Updated on 26-Dec-2024 20:42:05

775 Views

In Java, the Comparator interface is used to define a custom ordering for objects in various Collections. This can be particularly useful when you need to find the maximum or minimum element based on specific criteria rather than natural ordering. One common use case is finding the maximum value in a collection using a comparator. Java provides the Collections utility class, which includes the max() and min() methods that accept a comparator to perform customized comparisons. In this article, we will explore how to use these methods to find the maximum value from a list using a comparator. Finding Maximum ... Read More

Advertisements