Found 9150 Articles for Object Oriented Programming

Java Program to add minutes to current time using Calendar.add() method

Samual Sam
Updated on 17-Jul-2024 13:12:22

3K+ Views

Java provides a built-in class called Calendar that allows developers to work with dates and times in their applications. The Calendar class provides various methods to manipulate dates and times, such as adding or subtracting days, months, years, hours, minutes, and seconds. Problem Statement Write a Java program to increment the current time by 10 minutes using the Calendar class. Output Current Date = Thu Nov 22 16:24:27 UTC 2018 Updated Date = Thu Nov 22 16:34:27 UTC 2018 Steps to add minutes to current time using Calendar.add() method Below are the steps to add minutes to current time using ... Read More

Java program to subtract hours from current time using Calendar.add() method

Samual Sam
Updated on 20-Nov-2024 22:39:59

763 Views

In this article, we use the Calendar class in Java to work with dates and times. First, it retrieves the current date and time and displays it. Then, the program increments the current time by 5 hours using the add() method and displays the updated date and time. This is a simple example of modifying and handling date-time values programmatically. Subtracting hours from current time using Calendar.add() method. Calendar class The Java Calendar class is found in the java.util package. It is an abstract class that provides methods for converting a specific moment in time into various calendar fields such ... Read More

Add hours to current time using Calendar.add() method in Java

Aishwarya Naglot
Updated on 17-Jul-2025 18:12:08

2K+ Views

What is the Calendar.add() method in Java? The add() method belongs to the java.util.Calendar class. This method is used to add or subtract a specified amount of time to any field of the calendar. For example, we can add months, years, days, hours, minutes, etc., to the current date or current time using this method. Syntax Following is the syntax of the calendar.add() method: calendar.add(Calendar.HOUR, amount); Here, Calendar.HOUR is a constant that represents the hour field in the calendar, and amount is the number of hours you want to add to the current date. Adding Hours to Current Time ... Read More

Subtract days from current date using Calendar.DATE in Java

karthikeya Boyini
Updated on 25-Jun-2020 11:37:55

2K+ Views

Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us subtract the days using the add() method and Calendar.DATE constant. Set a negative value here since we are decrementing.calendar.add(Calendar.DATE, -2);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing days by 2       calendar.add(Calendar.DATE, -2);       System.out.println("Updated Date = " + ... Read More

Java program to find maximum of three numbers

karthikeya Boyini
Updated on 04-Nov-2024 18:42:13

30K+ Views

In this article, we will learn how to find the maximum among three numbers using an if-else statement in Java. The if-else construct allows us to evaluate conditions and execute different blocks of code based on whether the conditions are true or false. We will check the values of three numbers to determine the largest by using comparison operators and control flow statements. Problem StatementGiven three integer values, write a Java program to find the maximum among them using if-else statements. Input num1 = 15, num2 = -5, num3 = 7 Output 15 is the maximum number. Steps to ... Read More

Java Program to Match Dates

karthikeya Boyini
Updated on 25-Jun-2020 11:49:33

182 Views

Firstly, we have considered the following two dates.SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = s.parse("2018-10-15"); Date d2 = s.parse("2018-11-10");Now, use the compareTo() method to compare both the dates. The results are displayed on the basis of the return value.if (d1.compareTo(d2) > 0) {    System.out.println("Date1 is after Date2!");    } else if (d1.compareTo(d2) < 0) {       System.out.println("Date1 is before Date2!");    } else if (d1.compareTo(d2) == 0) {       System.out.println("Date1 is equal to Date2!");    } else {       System.out.println("How to get here?"); }Example Live Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ... Read More

Java Program to Match Zip Codes

Samual Sam
Updated on 23-Nov-2024 03:49:21

402 Views

In this article, we will learn how to validate U.S. zip codes using a regular expression in Java. The program checks if a given string is a valid U.S. zip code, either in the standard five-digit format or the extended nine-digit format.Zip Code Format In the U.S., zip codes are five digits, with each digit representing a specific part of the United States.Let’s say we have the following zip code. String zipStr = "12345"; Now, set the following regular expression to match zip codes in America. String reg = "^[0-9]{5}(?:-[0-9]{4})?$"; Matching (Validating) a ZIP CodeThe following are the steps to ... Read More

Java program to remove the leading and trailing quotes from a string

karthikeya Boyini
Updated on 19-Sep-2024 22:00:16

2K+ Views

In Java, handling strings with quotes can be managed by checking and manipulating the string’s start and end. This example demonstrates how to remove double quotes from both the beginning and end of a string. Problem Statement Given a string enclosed in double quotes, write a Java program to remove these enclosing quotes. The result must be the original string without the enclosing double quotes. Input String with double quotes= "Demo Text" Output String after removing double quotes = Demo Text Steps to remove the leading and trailing quotes from a string Below are the steps to remove the leading ... Read More

Shift left in a BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 10:59:45

191 Views

To shift left in a BigInteger, use the shiftLeft() method.The java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this

Java Program to shift bits in a BigInteger

karthikeya Boyini
Updated on 25-Jun-2020 11:01:12

130 Views

To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.shiftLeft() methodThe java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this > n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Advertisements