Found 4336 Articles for Java 8

Java Program to get Milliseconds between two time instants

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

2K+ Views

At first, create two time instants −Instant time1 = Instant.now(); Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS);Use between() to get the milliseconds between two time instants −Duration duration = Duration.ofSeconds(13); Instant i = time1.plus(duration); System.out.println("Milliseconds between two time instants = "+ChronoUnit.MILLIS.between(time1, time2));Example Live Demoimport java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Demo {    public static void main(String[] args) {       Instant time1 = Instant.now();       Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS);       Duration duration = Duration.ofSeconds(13);       Instant i = time1.plus(duration);       System.out.println("Milliseconds between two time instants =       ... Read More

Java Program to convert java.util.Date to LocalDate

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

169 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

278 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

385 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

324 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

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

637 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

116 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

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

410 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 min() will eventually give you the minimum value, but with reverseOrder(), it reverses the result −Comparator comp = Collections.reverseOrder(); System.out.println("Maximum element = "+Collections.min(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, 70 };     ... Read More

Advertisements