Found 9316 Articles for Object Oriented Programming

Java Program to convert Date into milliseconds

karthikeya Boyini
Updated on 27-Jun-2020 08:48:17

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.Example Live Demoimport 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

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

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

468 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; 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-19Read More

Java Program to convert from a java.util.Date Object to a java.sql.Date Object

karthikeya Boyini
Updated on 27-Jun-2020 08:49:36

466 Views

First, let us create a java.util.Date object.// util object java.util.Date utilObj = new java.util.Date();Now, create a java.sql.Date object.java.sql.Date sqlObj = new java.sql.Date();Converting from a java.util.Date object to a java.sql.Date object.java.sql.Date sqlObj = new java.sql.Date(utilObj.getTime());The following is an example.Example Live Demoimport java.util.Date; public class Example {    public static void main(String args[]) {       // util object       java.util.Date utilObj = new java.util.Date();       // sql object       java.sql.Date sqlObj = new java.sql.Date(utilObj.getTime());       System.out.println("Util Date = " + utilObj);       System.out.println("SQL Date = " + sqlObj);    } }OutputUtil ... Read More

Escape sequences in Java

Deepti S
Updated on 29-Aug-2023 15:29:23

4K+ Views

Escape sequences are a unique kind of character that are used to indicate a different way of interpreting a group of characters. An escape sequence in Java is a character that is preceded by a backslash (). An escape sequence is treated by the Java compiler as a single character with unique meaning. Java frequently employs the following escape sequences: \t: Adds a new tab : Adds a new line \r: Adds a carriage return ': Adds a single quote ": Adds a double quote \: Adds a backslash These escape sequences can be used to control the ... Read More

Java Program to split a string using Regular Expression

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

843 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 void main(String[] args) throws Exception {       String str = "{Java is a programming language} {James Gosling developed it.}";       String[] strSplit = str.split("[{}]");       System.out.println("Splitting String...");       for (int i = 0; i < strSplit.length; i++)       System.out.println(strSplit[i]);   ... Read More

Java Program to split a string with dot

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

419 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.Example Live Demopublic 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("\.");       ... Read More

Java Program to display double and single quote in a string

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 as at the end.String str2 = "\"This is it\"!";The following is an example.Example Live Demopublic 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 check if a string is empty or not

karthikeya Boyini
Updated on 27-Jun-2020 06:44:46

265 Views

The following is our string −String str = "";We will check whether it is empty or not using the isEmpty() method. The result will be a boolean −str.isEmpty()The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "";       System.out.println("String is empty: "+str.isEmpty());    } }OutputString is empty: true

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

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++; index = index + subString.length(); }The following is an example.Example Live Demopublic 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

Get the substring before the last occurrence of a separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:46:18

5K+ Views

We have the following string with a separator.String str = "David-Warner";We want the substring before the last occurrence of a separator. Use the lastIndexOf() method.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.lastIndexOf(separator); System.out.println("Substring before last separator = "+str.substring(0, sepPos));The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "David-Warner";       String separator ="-";       int sepPos = str.lastIndexOf(separator);       if (sepPos == -1) {          System.out.println("");     ... Read More

Advertisements