- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34485 Articles for Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
To work with the GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create a Date object.Date d = new Date();Now, create an object and set the time using the setTime() method.GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d);The following is an example.Example Live Demoimport java.util.Date; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { Date d = new Date(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d); System.out.println(d); } }OutputMon Nov 19 16:11:31 UTC 2018
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
145 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 { public static void main(String[] a) { GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US); System.out.println(cal); } }Outputjava.util.GregorianCalendar[time=1542643323673, areFieldsSet=true, areAllFieldsSet=true, lenient=true, zone=sun.util.calendar.ZoneInfo[id="America/New_York", offset=-18000000, dstSavings=3600000, useDaylight=true, transitions=235, lastRule=java.util.SimpleTimeZone[id=America/New_York, offset=-18000000, dstSavings=3600000, useDaylight=true, startYear=0, startMode=3, startMonth=2, startDay=8, startDayOfWeek=1, startRead More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted.The java.util.GregorianCalendar class in Java is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.Import the following package to work with GregorianCalendar class.import java.util.GregorianCalendar;The following are the constructors.Sr.No.Constructor & Description1GregorianCalendar() This constructs a default GregorianCalendar using the current time in the default time zone with the default locale.2GregorianCalendar(int year, int month, int dayOfMonth) This constructs a GregorianCalendar with the given ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
689 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
219 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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