Found 34494 Articles for Programming

Create a Date object using the Calendar class in Java

karthikeya Boyini
Updated on 27-Jun-2020 08:46:51

1K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Now, let us create an object of Calendar class.Calendar calendar = Calendar.getInstance();Set the date, month and year.calendar.set(Calendar.YEAR, 2018); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.DATE, 18);Create a Date object using Calendar class.java.util.Date dt = calendar.getTime();The following is an example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       // Set year, month and date       calendar.set(Calendar.YEAR, 2018);       calendar.set(Calendar.MONTH, 11);       calendar.set(Calendar.DATE, 18);       // util date object       java.util.Date dt = ... Read More

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

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 Demoimport 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

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

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; 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

464 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

839 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

418 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

263 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

Advertisements