Found 9326 Articles for Object Oriented Programming

Java program to reverse each word in a sentence

Samual Sam
Updated on 25-Jun-2020 13:07:03

856 Views

Each word in a sentence can be reversed and the sentence displayed with the words in the same order as before. An example of this is given as follows −Original sentence = an apple is red Modified sentence = na elppa si derA program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String[] args) { String str = "the sky is blue"; System.out.println("The original string is: " + str); String strWords[] = str.split("\s"); String rev = ""; for(String sw : strWords) { StringBuilder sb = new StringBuilder(sw); sb.reverse(); rev += sb.toString() + " "; ... Read More

Add seconds to current date using Calendar.add() method in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:11:53

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 increment the seconds using the calendar.add() method and Calendar.SECOND constant.calendar.add(Calendar.SECOND, 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());       // Add 15 seconds to current date       calendar.add(Calendar.SECOND, 15);       System.out.println("Updated Date = " + calendar.getTime());    } ... Read More

Java Program to subtract months from current date using Calendar.add() method

Samual Sam
Updated on 25-Jun-2020 13:15:28

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 months using the calendar.add() method and Calendar.MONTH constant. Set a negative value since we are decrementing.calendar.add(Calendar.MONTH, -10);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 8 months from current date calendar.add(Calendar.MONTH, -8); System.out.println("Updated Date = " + calendar.getTime()); } }OutputCurrent Date = Thu Nov 22 16:37:42 UTC 2018 ... Read More

Add months to current date using Calendar.add() method in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:16:15

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

Subtract minutes from current time using Calendar.add() method in Java

Samual Sam
Updated on 25-Jun-2020 12:16:40

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

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

Samual Sam
Updated on 25-Jun-2020 12:17:41

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 increment the minutes using the calendar.add() method and Calendar.HOUR_OF_DAY constant.calendar.add(Calendar.MINUTE, 10);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 10 minutes to current date       calendar.add(Calendar.MINUTE, 10);       System.out.println("Updated Date = " + calendar.getTime());    } ... Read More

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

Samual Sam
Updated on 25-Jun-2020 12:29:02

448 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

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

Samual Sam
Updated on 25-Jun-2020 11:36:40

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

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 reverse an array in groups of given size

Samual Sam
Updated on 25-Jun-2020 11:39:28

2K+ Views

An array can be reversed in groups of given size by reversing the subarrays of the required size. An example of this is given as follows.Array = 1 2 3 4 5 6 7 8 9 10 Group size = 3 Modified array = 3 2 1 6 5 4 9 8 7 10A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) {       int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};       int size = 4;       int ... Read More

Advertisements