In this article, you'll learn how to convert a Time object into a String using the toString() method from the java.sql.Time class. This method allows us to easily transform a Time object into its JDBC escape format, which can then be handled as a string. //Retrieving the Time object Time timeObj = rs.getTime("DeliveryTime"); //Converting the Time object to String format String time = timeObj.toString(); Steps for using Java sql.Time toString() Method Following are the steps for using Java sql.Time toString() Method − Create a MySQL table named dispatches. Insert some sample records into ... Read More
In this article, we will learn how to write an array of strings to a text file using Java. The program demonstrates how to use the FileWriter class to create and write a file. This method helps save data to a text file for future retrieval or processing. FileWriter class: This class extends the OutputStreamWriter class and is used to write streams of characters to a file. It provides methods to write text data easily and efficiently, making it a key tool for handling file output operations in Java. Problem Statement Write a program in Java that writes an array ... Read More
In this article, we will learn to implement insertion sort in Java. The insertion sort is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element to be inserted in this sorted sub-list must find its appropriate place and then be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). Problem Statement For a given array write a program in Java to implement ... Read More
In this article, we are given an integer value and the task is to count the total number of set bits of the given integer. For this task, we need to convert the given value into its corresponding binary representation. The binary number of an integer value is represented as the combination of 0's and 1's. Here, the digit 1 is known as the Set bit in the terms of the computer. Problem Statement Write a program in Java to count set bits in an integer − Input int num = 10 Output binary representation = 1010 set bit ... Read More
Suppose a date is given in LocalDate format, our task is to write a Java program that converts it into java.util.Date. For this problem, we need to add time information to the LocalDate by combining it with a time, such as midnight, or a timezone. Both LocalDate and java.util.Date classes are used to represent a date in Java. But, the LocalDate class, introduced with the release of Java8, represents a date without timezone information, whereas, the Date class is mutable and used for representing date and time with timezone. Let's understand the problem statement with an example. Example Scenario: Input: ... Read More
Initialization of a Java HashMap means creating an instance of the HashMap class and adding elements to it. The standard way to initialize a HashMap is by using the new keyword. However, we will use the lambda expression in this article. Lambda expressions are methods without any names. It was introduced with the release of Java8. And, HashMap is a class of Java Collection Framework that implements Map Interface. It stores its element in key-value pairs. The Key is an object that is used to retrieve value associated with it. Example 1 The following Java program explains how to initialize ... Read More
Given a square whose length of all its sides are l, write a Java program to find its area. A square is a rectangle whose length and breadth are same. Therefore, area of this type of rectangle is the square of its length. To calculate the area of square in Java, you simply need to multiply the given length of square with the length itself and store the result in another variable. Java Program to Find The Area of a Square A Java program that illustrates how to find the area of the square is shown below − public class ... Read More
In this article, we are going to use various approaches for formatting the months using different libraries of Java programming language. There are several ways to display months. Sometimes the months are written as numbers, sometimes the months are written in long form or they are written in short forms. Using java.time.Month In this approach, the months are printed by specifying the month number that starts from the number 1. For instance -> Month.of(1) will give JANUARY. Example In this Java program, we are printing all 12 months name. import java.time.Month; public class ShowMonth { ... Read More
To find the smallest from three given numbers using ternary operator, create a temporary variable to store the result of first comparison operation between two numbers. Then, perform second comparison operation between the result of first operation (i.e. temp) and third number to get the final result. Let's understand the problem statement with an example − Example Scenario: Input: nums = 20, 35, 10; Output: res = 10 What is Ternary Operator? The conditional operator in Java is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal ... Read More
For a given string, say "str", write a Java program to check if it contains each character from a specified character array. If it contains print "found" otherwise "not found". A String in Java is a class that represents a contiguous block of characters. Example Scenario: Input 1: String str = "pqrst"; Input 2: char[] chSearch = {'p', 'q', r'}; Output: res = Character p, q and r found in given string In the string "pqrst", we are searching for the character set {'p', 'q', r'}. Using Iteration Here, use for loop to iterate till the length of the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP