
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
Found 9150 Articles for Object Oriented Programming

3K+ Views
In general, Java Array is a collection of homogeneous elements and Streams are sequence of objects from a source, which supports aggregate operations. We can create a IntStream object holding a sequence of integer values. To convert the integer array to an IntStream object you need to use the Arrays.stream() method. IntStream: The IntStream extends the BaseStream interface. It defines the stream of primitive integer value. We can import the IntStream class from java.util package. The Arrays.stream() method The Arrays.stream() method creates a sequential stream from an array. It is a static method in the Arrays. Following is the syntax ... Read More

1K+ Views
In this article, we will learn to get the difference between two time zones by seconds using Java. We'll use LocalDateTime, ZoneId, and ZonedDateTime classes from java.time package to achieve this.Problem Statement Write a program in Java to get the difference between two time zones by seconds − Output Difference between two time zones in seconds = 28800 Steps to get the difference between two time zones by seconds Following are the steps to get the difference between two time zones by seconds − First, we will import the necessary classes LocalDateTime, ZoneId, and ZonedDateTime from ... Read More

211 Views
To create custom DateTime formatter, use DateTimeFormatter. Let us first see for Time −DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter();For Date −dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("/") .appendValue(ChronoField.MONTH_OF_YEAR) .appendLiteral("/") .appendValue(ChronoField.DAY_OF_MONTH) .toFormatter();Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; public class Demo { public static void main(String[] args) { DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter(); System.out.println("Time = "+dtFormat.format(LocalDateTime.now())); dtFormat = new DateTimeFormatterBuilder() ... Read More

185 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

1K+ Views
In this article, we will learn to convert a date object to an Instant object in Java. The Instant class represents a specific moment on the timeline, often used for precise time calculations. We'll use the toInstant() method provided by the Date class to achieve this conversion Problem Statement Write a program in Java to convert date to instant. Input >Date = Thu Apr 18 23:32:07 IST 2019 Output java.util.Date to Instant = 2019-04-18T18:02:07.330Z Steps to convert java.util.Date to Instant Following are the steps to convert date to instant − Start by importing the Instant class ... Read More

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]

269 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

214 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