Found 34494 Articles for Programming

Checking for a Leap Year using GregorianCalendar in Java

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

274 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 Demoimport java.text.ParseException; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] args) throws ParseException {       GregorianCalendar gcal = new GregorianCalendar();       System.out.println("Is it a leap year? "+gcal.isLeapYear(2012));    } }OutputIs it a leap year? trueLet us see another example.Example Live Demoimport java.text.ParseException; import java.util.GregorianCalendar; ... Read More

Get week of month and year using Java Calendar

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 = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // date information       System.out.println("Date Information..........");       System.out.println("Year = " + cal.get(Calendar.YEAR));       System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1));       System.out.println("Date = " + ... Read More

Get time in milliseconds using Java Calendar

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) {       Calendar cal = Calendar.getInstance();       System.out.println("Milliseconds =" + cal.getTimeInMillis());    } }OutputMilliseconds =1542636999896

Display entire date time with milliseconds in Java

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

218 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 complete example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // date information       System.out.println("Date Information..........");       System.out.println("Year = ... Read More

Get the last day of month in Java

Samual Sam
Updated on 27-Jun-2020 09:05:14

13K+ Views

The getActualMaximum() method returns the maximum value that the specified calendar field could have, based on the time value of this Calendar.Import the following package to work with Calendar class in Java, import java.util.Calendar;Create a calendar object.Calendar cal = Calendar.getInstance();Use the getActualMaximum() method to get the last day of the month.int res = cal.getActualMaximum(Calendar.DATE);The following is an example.Example Live Demoimport java.util.Calendar; public class Main {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       int res = cal.getActualMaximum(Calendar.DATE);       System.out.println("Today's Date = " + cal.getTime());       System.out.println("Last Date of ... Read More

Get current time information in Java

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) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // time information       System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR));     ... Read More

Display Month of Year using Java Calendar

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 Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };       System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]); ... Read More

Display Day Name of Week using Java Calendar

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 java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Day: " + (calendar.get(Calendar.DATE)));       System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));       System.out.println("Year: " + (calendar.get(Calendar.YEAR)));       String[] days = new String[] ... Read More

Get Day Number of Week in Java

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

10K+ 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 static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println(calendar.getTime().toString());       int day = calendar.get(Calendar.DAY_OF_WEEK);       System.out.println("Day: " + day);       int hour = calendar.get(Calendar.HOUR_OF_DAY);       System.out.println("Hour: " + hour);       int minute = ... Read More

Java Program to display Current Time in another Time Zone

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 void main(String[] args) {       Calendar cal = Calendar.getInstance();       System.out.println("Europe/Sofia TimeZone...");       cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia"));       System.out.println("Hour = " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Minute = " + cal.get(Calendar.MINUTE));       System.out.println("Second = " + cal.get(Calendar.SECOND));       System.out.println("Millisecond = " ... Read More

Advertisements