Karthikeya Boyini has Published 2193 Articles

Parse string date value input in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 09:29:14

231 Views

UseSimpleDateFormat('dd-MMM-yy') for string date.Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");For above class, do not forget to import the following package, else an error would be visible.import java.text.SimpleDateFormat;Now, parse the date.Date dt = (Date) dateFormatter.parseObject("20-Nov-18");Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Main {    public static void main(String[] argv) throws Exception ... Read More

Specify the TimeZone explicitly for Gregorian Calendar in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 09:08:31

224 Views

To specify the TimeZone explicitly, use the getTimeZone() method of the TimeZone class. For Locale and TimeZone, we have imported the following packages.import java.util.Locale; import java.util.TimeZoneLet us specify for timezone America/New_York.GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US);The following is an example.Example Live Demoimport java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; public class Demo { ... Read More

Display entire date time with milliseconds in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 09:03:28

324 Views

Import the following package to work with Calendar class in Java, import java.util.Calendar;To display the entire day time, firstly create a Calendar object.Calendar cal = Calendar.getInstance();Display the entire date time using the fields shown below −// DATE Calendar.YEAR Calendar.MONTH Calendar.DATE // TIME Calendar.HOUR_OF_DAY Calendar.HOUR Calendar.MINUTE Calendar.SECOND Calendar.MILLISECONDThe following is the ... Read More

Get week of month and year using Java Calendar

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 09:01:18

1K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Create a Calendar class object.Calendar cal = Calendar.getInstance();Now, get the week of month and year using the following fields.Calendar.WEEK_OF_MONTH Calendar.WEEK_OF_YEARThe following is an example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal ... Read More

Checking for a Leap Year using GregorianCalendar in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 08:58:28

370 Views

The GregorianCalendar.isLeapYear() method determines if the given year is a leap year. Returns true if the given year is a leap year.Firstly, import the following package to work with GregorianCalendar class.import java.util.GregorianCalendar;Now, check for a year by adding it as a parameter in the isLeapYear() method.gcal.isLeapYear(2012)The following is an example.Example Live ... Read More

Get Day Number of Week in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 08:57:07

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.Example Live Demoimport java.util.Calendar; public class Demo {    public ... Read More

Display Month of Year using Java Calendar

karthikeya Boyini

karthikeya Boyini

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

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

Get current time information in Java

karthikeya Boyini

karthikeya Boyini

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

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

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

karthikeya Boyini

karthikeya Boyini

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

9K+ 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

Advertisements