Java Articles

Page 86 of 450

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 389 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++; index = index + subString.length(); }The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Learning never ends! Learning never stops!";       System.out.println("String: "+str);       int subStrCount = 0;       String subString = ...

Read More

Java Program to display double and single quote in a string

Samual Sam
Samual Sam
Updated on 11-Mar-2026 6K+ 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 as at the end.String str2 = ""This is it"!";The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str1 = "This is Jack's mobile";       String str2 = ""This is it"!";       System.out.println("Displaying Single Quote: "+str1);   ...

Read More

Java Program to split a string with dot

Samual Sam
Samual Sam
Updated on 11-Mar-2026 675 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 under the split methods parameter.The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Java is a programming language. James Gosling developed it.";       System.out.println("String: "+str);       String[] strSplit = str.split("\.");       System.out.println("Splitting ...

Read More

Convert the Current Time to a java.sql.Date Object

Samual Sam
Samual Sam
Updated on 11-Mar-2026 704 Views

Firstly, create a Calendar class object.Calendar calendar = Calendar.getInstance();Now, import the following package.import java.sql.Date;Using a Date class now and creating an object would belong to the above package. Convert the current time to the java.sql.Date Object.Date sqlDate = new Date((calendar.getTime()).getTime());The following is an example.Exampleimport java.util.Calendar; import java.sql.Date; import java.text.ParseException; public class Demo {    public static void main(String[] args) throws ParseException {       Calendar calendar = Calendar.getInstance();       // object       Date sqlDate = new Date((calendar.getTime()).getTime());       System.out.println(sqlDate);    } }Output2018-11-19

Read More

Java Program to convert Date into milliseconds

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

Import the following package to work with Date class.import java.util.Date;No create a Date object.Date d = new Date();Let us convert the current date to milliseconds.d.getTime()The following is an example.Exampleimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.println("Date = " + d);       System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT = " + d.getTime());    } }OutputDate = Mon Nov 19 06:30:11 UTC 2018 Milliseconds since January 1, 1970, 00:00:00 GMT = 1542609011369

Read More

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 534 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.Exampleimport java.text.ParseException; public class Demo {    public static void main(String[] args) throws ParseException {       java.util.Date utilDate = new java.util.Date();       java.sql.Date dt = new java.sql.Date(utilDate.getTime());       System.out.println("Today's date: "+dt);       java.util.GregorianCalendar cal = new java.util.GregorianCalendar();       cal.setTime(dt);     ...

Read More

Get Day Number of Week in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 11K+ Views

To get the day of the week, use Calendar.DAY_OF_WEEK.Firstly, declare a calendar object and get the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toString());Now, fetch the day of the week in an integer variable.int day = calendar.get(Calendar.DAY_OF_WEEK);The following is the final example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println(calendar.getTime().toString());       int day = calendar.get(Calendar.DAY_OF_WEEK);       System.out.println("Day: " + day);       int hour = calendar.get(Calendar.HOUR_OF_DAY);       System.out.println("Hour: " + hour);       int minute = calendar.get(Calendar.MINUTE); ...

Read More

Display Day Name of Week using Java Calendar

Samual Sam
Samual Sam
Updated on 11-Mar-2026 4K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Using the Calendar class, create an object.Calendar calendar = Calendar.getInstance();Now, create a string array of the day names.String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };Display the day name.days[calendar.get(Calendar.DAY_OF_WEEK) - 1]The following is the final example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Day: " + (calendar.get(Calendar.DATE)));       System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));       System.out.println("Year: " + (calendar.get(Calendar.YEAR)));       String[] days = new String[] { ...

Read More

Display Month of Year using Java Calendar

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Using the Calendar class, create an object.Calendar calendar = Calendar.getInstance();Now, create a string array of the month names.String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };Display the month name.month[calendar.get(Calendar.MONTH)]The following is an example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };       System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]);   ...

Read More

Get current time information in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 311 Views

Import the following package for to work with Calendar class in Java, import java.util.Calendar;Create a calendar class now.Calendar cal = Calendar.getInstance();To display entire time information, use the following fields.cal.get(Calendar.HOUR_OF_DAY) cal.get(Calendar.HOUR) cal.get(Calendar.MINUTE) cal.get(Calendar.SECOND) cal.get(Calendar.MILLISECOND)The following is the final example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // time information       System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR));       ...

Read More
Showing 851–860 of 4,496 articles
« Prev 1 84 85 86 87 88 450 Next »
Advertisements