Found 4335 Articles for Java 8

Java Program to convert java.util.Date to java.time.LocalDateTime

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

114 Views

First, set the date −java.util.Date date = new Date();Now, convert the above Date to java.time.LocalDateTime −java.time.LocalDateTime dateTime =    java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());Exampleimport 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);       java.time.LocalDateTime dateTime =          java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());       System.out.println("LocalDateTime = "+dateTime);    } }OutputDate = Thu Apr 18 23:39:34 IST 2019 LocalDateTime = 2019-04-18T23:39:34.400

Java Program to convert java.util.Date to Instant

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

713 Views

First, set the date −java.util.Date date = new java.util.Date();Now, convert the above date to Instant −Instant instant = date.toInstant();Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       java.util.Date date = new java.util.Date();       System.out.println("Date = "+date);       Instant instant = date.toInstant();       System.out.println("java.util.Date to Instant = "+instant);    } }OutputDate = Thu Apr 18 23:32:07 IST 2019 java.util.Date to Instant = 2019-04-18T18:02:07.330Z

Java Program to fill an array of characters from user input

Samual Sam
Updated on 18-Jun-2024 16:45:18

14K+ Views

For user input, use the Scanner class with System.in. After getting the input, convert it to character array −char[] a = s.next().toCharArray();Now, display it until the length of the character array i.e. number of elements input by the user −for (int i = 0; i < a.length; i++) {    System.out.println(a[i]); }To fill an array of characters from user input, use Scanner class.Exampleimport java.util.Scanner; public class Demo {    public static void main(String args[]) {       Scanner s = new Scanner(System.in);       System.out.println("First add some characters...");       char[] a = s.next().toCharArray();       ... Read More

Java program to double the size of an array

karthikeya Boyini
Updated on 02-Jul-2024 11:21:24

3K+ Views

In this article, we will learn how to double the size of an array in Java. This involves creating a new array with twice the length of the original array and copying the elements from the original array to the new, larger array. Problem Statement Create an initial array with predefined integer values and determine its length. Then, create a new array with double the length of the original array, copy the elements from the original array to the new one, and print the lengths of both the original and the new arrays to verify the resizing operation. Steps to ... Read More

Java Program to generate random numbers with no duplicates

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

3K+ Views

For random numbers in Java, create a Random class object −Random randNum = new Random();Now, create a HashSet to get only the unique elements i.e. no duplicates −Setset = new LinkedHashSet();Generate random numbers with Random class nextInt −while (set.size() < 5) {    set.add(randNum.nextInt(5)+1); }Exampleimport java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class Demo {    public static void main(final String[] args) throws Exception {       Random randNum = new Random();       Setset = new LinkedHashSet();       while (set.size() < 5) {          set.add(randNum.nextInt(5)+1);       }       System.out.println("Random numbers with no duplicates = "+set);    } }OutputRandom numbers with no duplicates = [2, 4, 1, 3, 5]

Java Program to shuffle an array using list

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

129 Views

Let us first create a string array and convert it to a list. After that, the shuffle would be performed.Following is the string array −String str[] = {"A", "B", "C", "D", "E"};Convert the string array to list −Listlist = Arrays.asList(str);Now shuffle the array −Collections.shuffle(list);Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       String str[] = {"A", "B", "C", "D", "E"};       Listlist = Arrays.asList(str);       Collections.shuffle(list);       System.out.println("Shuffled the array using List = "+list.toString());    } }OutputShuffled the array using List = [C, ... Read More

Java Program to convert java.util.Date to any local date in certain timezone

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

130 Views

First, set the Date and ZoneId −Date date = new Date(); ZoneId zone = ZoneId.systemDefault();Now convert the java.util.date to localdate −date.toInstant().atZone(zone).toLocalDate() date.toInstant().atZone(zone).toLocalTime() date.toInstant().atZone(zone).getHour() date.toInstant().atZone(zone).getMinute() date.toInstant().atZone(zone).getSecond()Exampleimport java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       Date date = new Date();       ZoneId zone = ZoneId.systemDefault();       System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());       System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());       System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());       System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());       System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());    } }OutputLocalDate = 2019-04-18 LocalTime= 23:25:09.708 Hour = 23 Minute = 25 Seconds = 9Read More

How to get number of quarters between two dates in Java

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

465 Views

Let’s say we have the following two dates −LocalDate.of(2019, 3, 20); LocalDate.of(2019, 10, 25);To get the number of quarters between the above two dates, use the QUARTER_YEARS −IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20),LocalDate.of(2019, 10, 25));Exampleimport java.time.LocalDate; import java.time.temporal.IsoFields; public class Demo {    public static void main(String[] args) {       long quarters =          IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20), LocalDate.of(2019, 10, 25));       System.out.println("Quarters between the two dates = " + quarters);    } }OutputQuarters between the two dates = 2

How do I discover the Quarter of a given Date in Java?

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

2K+ Views

Let us first get the current date −LocalDate currentDate = LocalDate.now();Now, use the Calendar class and set the locale −Calendar cal = Calendar.getInstance(Locale.US);Now, get the month −int month = cal.get(Calendar.MONTH);Find the Quarter −int quarter = (month / 3) + 1;Exampleimport java.time.LocalDate; import java.util.Calendar; import java.util.Locale; public class Demo {    public static void main(String[] args) {       LocalDate currentDate = LocalDate.now();       System.out.println("Current Date = "+currentDate);       Calendar cal = Calendar.getInstance(Locale.US);       int month = cal.get(Calendar.MONTH);       int quarter = (month / 3) + 1;       System.out.println("Quarter = "+quarter);    } }OutputCurrent Date = 2019-04-12 Quarter = 2

Java Program to get Temporal Queries precision

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

97 Views

To get Temporal Queries precision, use the TemporalQuery interface with the precision() method of the TemporalQueries −TemporalQueryprecision = TemporalQueries.precision(); Get the precision for LocalDate: LocalDate.now().query(precision) Get the precision for LocalTime: LocalTime.now().query(precision) Get the precision for YearMonth: YearMonth.now().query(precision) Get the precision for Year: Year.now().query(precision)Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Year; import java.time.YearMonth; import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; public class Demo {    public static void main(String[] args) {       TemporalQueryprecision = TemporalQueries.precision();       System.out.println("TemporalQueries precision...");       System.out.println(LocalDate.now().query(precision));       System.out.println(LocalTime.now().query(precision));       System.out.println(LocalDateTime.now().query(precision));       System.out.println(YearMonth.now().query(precision));       System.out.println(Year.now().query(precision)); ... Read More

Advertisements