
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 7442 Articles for Java

135 Views
Let’s say we have the following string:String str = "YuBM787Nm";Now to display it as IntStream, use filter() and map() as shown below:int res = str.chars() .filter(Character::isDigit) .map(ch → Character.valueOf((char) ch)).sum();The following is an example to display string as IntStream:Examplepublic class Demo { public static void main(String[] args) { String str = "YuBM787Nm"; int res = str.chars() .filter(Character::isDigit) .map(ch -> Character.valueOf((char) ch)).sum(); System.out.println("String as IntStream = "+res); } }OutputString as IntStream = 166

911 Views
Use HTMLEditorKitt to change the font size with HTML. With that, use the JEditorPane setText() method to set HTML:HTMLEditorKit kit = new HTMLEditorKit(); editorPane.setEditorKit(kit); editorPane.setSize(size); editorPane.setOpaque(true); editorPane.setText(" This is a demo text with a different font!");The following is an example to change font size with HTML in Java Swing JEditorPane:Exampleimport java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.text.html.HTMLEditorKit; public class SwingDemo extends JFrame { public static void main(String[] args) { SwingDemo s = new SwingDemo(); s.setSize(600, 300); Container container = s.getContentPane(); s.demo(container, container.getSize()); ... Read More

290 Views
Let us first create a List:List emp = Arrays.asList( new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));We have class Employee with name, department and rank of employees.Now, create summary for the rank like count, average, sum, etc:IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));The following is an example to create IntSummaryStatistics from Collectors in Java:Exampleimport java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) throws Exception { List emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10)); IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → ... Read More

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

212 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

187 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