Found 34494 Articles for Programming

Java program to reverse an array upto a given position

Samual Sam
Updated on 25-Jun-2020 13:29:46

558 Views

An array can be reversed upto the given position pos and the remaining array is unchanged. An example of this is given as follows −Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Modified array = 6 5 4 3 2 1 7 8 9 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 n = arr.length;       int pos ... Read More

Java program to check for URL in a String

karthikeya Boyini
Updated on 25-Jun-2020 13:31:17

1K+ Views

A program can be created to check if a string is a correct URL or not. An example of an URL is given as follows −String = https://www.wikipedia.org/ The above string is a valid URLA program that demonstrates this is given as follows.Example Live Demoimport java.net.URL; public class Example {    public static boolean check_URL(String str) {    try {       new URL(str).toURI();       return true;    }catch (Exception e) {       return false;    } } public static void main(String[] args) {    String str = "http://www.wikipedia.org/";    System.out.println("String = " + str);   ... Read More

Java program to check whether a given string is Heterogram or not

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

337 Views

A string is a Heterogram if no letter of the alphabet occurs more than once in it. An example of this is given as follows −String = the big dwarf only jumpsThis string is a Heterogram as each alphabet in the string occurred only once.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       String str = "mango";       int n = str.length();       int alphaList[] = new int[26];       int flag = 1;       System.out.println("The string is: ... Read More

Check if a String is not empty ("") and not null in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:03:23

2K+ Views

Let’s say we have the following string −String myStr1 = "Jack Sparrow";Let us check the string now whether it is not null or not empty.if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty");Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "Jack Sparrow";       boolean res;       if(myStr != null || myStr.length() != 0) {          System.out.println("String is not null or not empty");       } else {          System.out.println("String is null or empty");       }    } }OutputString is not null or not empty

Java program to reverse each word in a sentence

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

860 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 08-Jul-2024 18:03:44

2K+ Views

When working with dates in Java, it's common to need to increment or decrement months. The Calendar class makes it easy to manipulate dates. This article shows how to decrement the current date by a specified number of months using the Calendar class. Problem Statement Given a Java program to decrement the current date by a specified number of months using the Calendar class. Output Current Date = Thu Nov 22 16:37:42 UTC 2018 Updated Date = Thu Mar 22 16:37:42 UTC 2018 Basic Approach Below are the steps to subtract months from current date using Calendar.add() ... 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 17-Jul-2024 13:12:22

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

Advertisements