Samual Sam has Published 2310 Articles

Get the Day of the Week from Today's Date in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 08:47:20

481 Views

To get the day of the week, use the Calendar.DAY_OF_WEEK.Firstly, let us get the current date.java.util.Date utilDate = new java.util.Date(); java.sql.Date dt = new java.sql.Date(utilDate.getTime());Now, using GregorianCalendar, set the time.java.util.GregorianCalendar cal = new java.util.GregorianCalendar(); cal.setTime(dt);The last step would display the day of the week as shown in the following example.Example Live ... Read More

Java Program to split a string using Regular Expression

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:51:09

1K+ Views

Let’s say we have the following string.String str = "{Java is a programming language} {James Gosling developed it.}";Above, the string is enclosed with parentheses. We can split a string with these parentheses using the split() method.String[] strSplit = str.split("[{}]");The following is an example.Example Live Demopublic class Demo {    public static ... Read More

Get the index of the first occurrence of a separator in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:46:57

1K+ Views

We have the following string with two separators.String str = "Tom-Hank-s";We want the index of the first occurrence of the separator.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.indexOf(separator);The following is an example.Example Live Demopublic class Demo {    public static ... Read More

Count how many times the substring appears in the larger String in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:45:40

360 Views

Let’s say we have the following string.String str = "Learning never ends! Learning never stops!";In the above string, we need to find out how many times the substring “Learning” appears.For this, loop until the index is not equal to 1 and calculate.while ((index = str.indexOf(subString, index)) != -1) { subStrCount++; ... Read More

Java Program to display double and single quote in a string

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:44:16

5K+ Views

The following are our strings with single and double quote.String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!";Above, for single quote, we have to mention it normally like.This is Jack's mobileHowever, for double quotes, use the following and add a slash in the beginning as well ... Read More

Java Program to split a string with dot

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:43:11

628 Views

Let’s say the following is our string.String str = "Java is a programming language. James Gosling developed it.";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split("\.");Above, we have split the string with dot as you can see ... Read More

Left pad a String with a specified String in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:39:31

194 Views

The following is our string.String str = "Jack";Now take a StringBuilder object.StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length to 20. The string that will be padded comes on the left.while (strBuilder.length() + str.length() < 20) { strBuilder.append("demo"); }The following is an example wherein we have ... Read More

Display Month in MMMM format in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:37:32

10K+ Views

The MMMM format for months is like entire month name: January, February, March, etc. We will use it like this.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMMM format = "+strMonth);Above, we have used the SimpleDateFormat class, ... Read More

Check if a String is whitespace, empty ("") or null in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:35:40

2K+ Views

Let’s say the following is our string.String myStr = "";Now, we will check whether the above string is whitespace, empty ("") or null.if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {    System.out.println("String is not null or not empty or not whitespace"); } else {    System.out.println("String is null or empty ... Read More

Java Program to format date time with Join

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:31:35

342 Views

To format date time with Join, set the date as a string and do not forget to add the delimeter.For delimeter “/” in the dateString.join("/", "11", "11", "2018");For delimeter “:” in the date.String.join(":", "10", "20", "20");The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) ... Read More

Advertisements