
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2681 of 3366

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

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

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]

281 Views
In this article, we will learn how to shuffle an array using a list in Java. To do so, we will be using: Using Collections.shuffle() Method Using Fisher-Yates shuffle Using Java Streams Using Collections.shuffle() Method Shuffling an array means randomly rearranging its elements. The Java Collections framework provides the Collections.shuffle() Method, which shuffles a list. Since this method shuffles the list randomly, the order of the resultant elements is different each time we use it. Example Following is the program to shuffle an array using a ... Read More

220 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

638 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

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

152 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

870 Views
In this article, we will learn to get the seconds since the beginning of the Java epoch. To get the seconds since the beginning of epochs, you need to use Instant. The method here used is ofEpochSecond() method. The epoch is the number of seconds that have elapsed since 00::00:00 Thursday, 1 January 1970. Get the seconds with ChronoUnit.SECONDS − long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS); Steps to retrieve seconds since Epoch Following are the steps to retrieve seconds since Epoch − First, we will import the Instant and ChronoUnit classes from the java.time and java.time.temporal packages. ... Read More

3K+ Views
Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat. In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date. Using LocalDate Class In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day ... Read More