Samual Sam has Published 2492 Articles

Get time in milliseconds using Java Calendar

Samual Sam

Samual Sam

Updated on 27-Jun-2020 09:02:34

687 Views

Create a Calendar object.Calendar cal = Calendar.getInstance();For using above Calendar class, do not forget to import the following package.import java.util.Calendar;Now, use the getTimeInMillis() method to get the time in milliseconds.cal.getTimeInMillis()The following is the final example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {     ... Read More

Display Day Name of Week using Java Calendar

Samual Sam

Samual Sam

Updated on 27-Jun-2020 08:55:52

3K+ 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.Example Live Demoimport ... Read More

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

Samual Sam

Samual Sam

Updated on 27-Jun-2020 08:49:02

465 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.Example Live Demoimport java.util.Calendar; import java.sql.Date; import java.text.ParseException; ... Read More

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

Samual Sam

Samual Sam

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

381 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 display Current Time in another Time Zone

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:58:23

792 Views

To display the current time in another time zone, use the TimeZone class. To use it, import the following package.import java.util.TimeZone;Firstly, set the TimeZone.cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia"));Now, display the date using the Calendar object.cal.get(Calendar.HOUR_OF_DAY) cal.get(Calendar.MINUTE) cal.get(Calendar.SECOND) cal.get(Calendar.MILLISECOND)The following is the final example.Example Live Demoimport java.util.Calendar; import java.util.TimeZone; public class Demo {    public static ... Read More

Java Program to split a string using Regular Expression

Samual Sam

Samual Sam

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

837 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

787 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

272 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

416 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

Advertisements