Compare Dates in Java: Check If One Date Is After Another

Shriansh Kumar
Updated on 01-Aug-2024 11:48:16

1K+ Views

In Java, there are a few scenarios where we are required to work with the date and time such as while developing a calendar application, attendance management system in Java and checking age of two persons. Also, the date is a way of keeping track of our time as it is an integral part of our daily lives. Therefore, Java provides classes like Date and LocalDate to work with date and time. In this article, we will discuss how to compare and check if a date is after another or not. Example Scenario 1 Input: dateOne = "2023-01-20"; ... Read More

Iterate Over Arrays Using For and Foreach Loop in Java

Shriansh Kumar
Updated on 01-Aug-2024 11:48:10

510 Views

In Java, both for loop and the for-each loop are used to iterate over each element of a stream or collection like arrays and ArrayList in order to perform desired operations. In this article, we will learn how to iterate over elements of an array using for and for-each loop. Here, the array is a data structure which stores a fixed-size sequential collection of elements of the same data type. Example Scenario 1 Input: String[] designations={"Ravi", "Riya", "Ashish"}; Output: Ravi, Riya, Ashish Example Scenario 2 Input: int[] designations = {2, 4, 5, 7}; Output: {2, 4, 5, 7} ... Read More

Java String Concatenation with Other Data Types

Shriansh Kumar
Updated on 01-Aug-2024 11:44:49

2K+ Views

In Java, string concatenation is the operation of joining two or more strings together. However, string concatenation can be performed with various primitive data types, not just with other strings. Two strings can be concatenated using concat() method of String class, but, to concatenate strings with other primitive data type you need to use the ‘+’ operator. The given data type will automatically converted to its string representation. Example Scenario: Input: String res = "Age: " + 45; Output: result = Age: 45 String Concatenation with int The int is a primitive datatype in Java which represents numeric data ... Read More

Java 8 Streams and Its Operations

Shriansh Kumar
Updated on 01-Aug-2024 11:44:01

2K+ Views

In Java, stream was introduced in Java 8. It represents a collection of elements and supports functional operations on those collections. Here, we are talking about collections like Array, List and Set. The Stream API simply channelizes the elements of sources through various built-in methods to return the desired result. In this article, we are going to discuss different operations that can be performed on the Java stream. Operations on Java 8 Streams There are two types of operations, we can perform on the Java streams − Intermediate Operations − They process the elements of an input stream ... Read More

Return Elements at Odd Position in a List in Java

Shriansh Kumar
Updated on 01-Aug-2024 11:43:36

925 Views

What is the Odd Position in a List? In Java, a List does not have predefined odd and even positions. However, if one considers the first position as index 0, then an odd position in the given List would be any index that is not divisible by 2. To determine if a position is odd, use the modulo operator (%).   How to Find Elements at Odd Position in a List? Below approaches are followed to find out the elements at the odd position in a list − By dividing index with 2 ... Read More

Remove All Elements from a Set in Java

Shriansh Kumar
Updated on 01-Aug-2024 11:42:49

2K+ Views

We are given a Set that contains multiple elements and our task is to write a Java program to remove elements from the Set. Here, the Set is an Sub Interface of the Java Collection Interface which defines a mathematical set. Removing Set Elements in Java To remove elements from a given set in Java, use the following ways − Using clear() method Using removeAll() method Using Iterator and remove() Using Java clear() method With the help of clear() method, we can remove all elements ... Read More

Find the Perimeter of a Rectangle in Java

Shriansh Kumar
Updated on 01-Aug-2024 11:03:16

1K+ Views

For a given rectangle of length l and width w, write a Java program to find its perimeter. The Perimeter of a rectangle is calculated by adding the lengths of all the sides of the rectangle. Below is a demonstration of a rectangle, a quadrilateral with four right angles (90°). The perimeter of a rectangle is the total length of the two lengths and two widths of the rectangle − Example Scenario: Input: length = 5, 8, 5, 8; Output: Perimeter = 26 On adding all the sides of rectangle, you will get perimeter. 5+8+5+8 = 26 Steps ... Read More

Check if JVM is 32 or 64 Bit

Shriansh Kumar
Updated on 01-Aug-2024 11:01:47

856 Views

In this article, we will learn how to determine whether the installed JVM in your system is 32 bit or 64 bit. For this operation, we use the getProperty() method of System class, which helps in retrieving system property of specified argument. In computer architecture, 32 bit and 64 bit refers to those components that operate on data in 32-bit units and 64-bit units respectively. 64 bit machines are said to be faster and more secure compare to the 32 bit machines. What is JVM? JVM is Java Virtual Machine that is responsible for executing the byte code. It is ... Read More

Format Time in AM/PM Format in Java

Shriansh Kumar
Updated on 01-Aug-2024 10:59:17

2K+ Views

Time formatting is a process of converting date and time values into human-readable strings with specific formats. The resulting string describes how date/time values should be represented (such as flat files or human-readable output). In this article, we will understand how to format time in AM-PM format. In 12-hour clock system, AM-PM format is used to define time. Here, AM stands for Ante meridiem which refers to the time before noon, whereas PM stands for Post meridiem which shows the time period after noon. Example Scenario Input: current_date = Thu Mar 17 16:04:31 IST 2024 Output: Current Time ... Read More

Find All Subsets of a String in Java

Shriansh Kumar
Updated on 01-Aug-2024 10:58:32

2K+ Views

In this article, we will understand how to find all the subsets of a string. In Java, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring. Example Scenario: Input: string = JVM; Output: The subsets of the string are: J, JV, JVM, V, VM, M Finding all Subsets of a String in Java Use the following approaches to find all subsets of a String in Java − Using nested for Loop ... Read More

Advertisements