- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 9321 Articles for Object Oriented Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
4K+ Views
Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the months using the calendar.add() method and Calendar.MONTH constant.calendar.add(Calendar.MONTH, 8);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()); // Add 8 months to current date calendar.add(Calendar.MONTH, 8); System.out.println("Updated Date = " + calendar.getTime()); } ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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 and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the minutes using the calendar.add() method and Calendar.MINUTE constant. Set a negative value since you want to decrease the minutes.calendar.add(Calendar.MINUTE, -15);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()); // Subtract 15 minutes from current date calendar.add(Calendar.MINUTE, -15); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
465 Views
Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the hours using the calendar.add() method and Calendar.HOUR_OF_DAY constant. Set a negative value since you want to decrease the hours.calendar.add(Calendar.HOUR_OF_DAY, +5);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()); // Incrementing hours by 5 calendar.add(Calendar.HOUR_OF_DAY, +5); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the hours using the calendar.add() method and Calendar.HOUR_OF_DAY constant.calendar.add(Calendar.HOUR_OF_DAY, +5);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()); // Incrementing hours by 5 calendar.add(Calendar.HOUR_OF_DAY, +5); System.out.println("Updated Date = " + calendar.getTime()); } }OutputCurrent Date ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
22K+ Views
The maximum among three numbers can be found using an if else statement. A Java program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String args[]) { int num1 = 15; int num2 = -5; int num3 = 7; if (num1 >= num2 && num1 >= num3) System.out.println( num1 + " is the maximum number."); else if (num2 >= num1 && num2 >= num3) System.out.println( num2 + " is the ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
26K+ Views
To check if a string is null or empty in Java, use the == operator.Let’s say we have the following strings.String myStr1 = "Jack Sparrow"; String myStr2 = "";Let us check both the strings now whether they are null or empty. Result will be a boolean.res = (myStr1 == null || myStr1.length() == 0); res = (myStr2 == null || myStr2.length() == 0); Example public class Demo { public static void main(String[] args) { String myStr1 = "Jack Sparrow"; String myStr2 = ""; boolean res; ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
129 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
177 Views
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})?$";Example Live Demopublic class Demo { public static void main(String[] args) { String zipStr = "12345"; // regular expression String reg = "^[0-9]{5}(?:-[0-9]{4})?$"; boolean res = zipStr.matches(reg); System.out.println("Is it a valid zip code in US? "+res); } }OutputIs it a valid zip code in US? True