Karthikeya Boyini has Published 2383 Articles

Display Month of Year using Java Calendar

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 08:53:25

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 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.Example Live ... Read More

Get current time information in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 08:50:59

172 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.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) ... Read More

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

karthikeya Boyini

karthikeya Boyini

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

462 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[]) ... Read More

Java Program to convert Date into milliseconds

karthikeya Boyini

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(); ... Read More

Create a Date object using the Calendar class in Java

karthikeya Boyini

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 {   ... Read More

Get the substring after the first occurrence of a separator in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:47:47

8K+ Views

We have the following string with a separator.String str = "Tom-Hanks";We want the substring after the first occurrence of the separator i.e.HanksFor that, first you need to get the index of the separator and then using the substring() method get, the substring after the separator.String separator ="-"; int sepPos = ... Read More

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

karthikeya Boyini

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 ... Read More

Java Program to check if a string is empty or not

karthikeya Boyini

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 = ... Read More

Line Separator in Java

karthikeya Boyini

karthikeya Boyini

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

3K+ Views

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "one" + System.lineSeparator() + ... Read More

Display Month in MMM format in Java

karthikeya Boyini

karthikeya Boyini

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

4K+ Views

The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc. Here, we will use the following.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMM format = "+strMonth);Above, we have used the SimpleDateFormat ... Read More

Advertisements