Found 9316 Articles for Object Oriented Programming

Java Program to display past date from GregorianCalendar

karthikeya Boyini
Updated on 27-Jun-2020 09:31:24

251 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Here is the object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, let us get the past date, using the add() method with a negative value.// past date cal.add((GregorianCalendar.DATE), -1);Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();       System.out.println("Current date: " + cal.getTime());       // past date       cal.add((GregorianCalendar.DATE), -1);       System.out.println("Modified date (Previous Date): " + cal.getTime());    } }OutputCurrent date: Mon Nov 19 18:01:37 UTC 2018 Modified date (Previous Month): Fri ... Read More

Java Program to display previous year from GregorianCalendar

Samual Sam
Updated on 27-Jun-2020 09:32:26

114 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous year.cal.add((GregorianCalendar.YEAR), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();       System.out.println("Current date: " + cal.getTime());       // previous year       cal.add((GregorianCalendar.YEAR), -1);       System.out.println("Modified date: " + cal.getTime());    } }OutputCurrent date: Mon Nov 19 18:05:49 UTC 2018 Modified date: Sun Nov 19 18:05:49 UTC 2017

Modify Date and Time from GregorianCalendar in Java

karthikeya Boyini
Updated on 27-Jun-2020 09:33:19

690 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Firstly, let us display the current date and time.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime());Now, modify the date. Here we are adding two days to the month using the add() method.cal.add((GregorianCalendar.MONTH), 2);Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();       System.out.println("Current date: " + cal.getTime());       cal.add((GregorianCalendar.MONTH), 2);       System.out.println("Modified date: " + cal.getTime());    } }OutputCurrent date: Mon Nov 19 17:52:55 UTC 2018 Modified date: Sat Jan ... Read More

Java Program to get the day of the week from GregorianCalendar

Samual Sam
Updated on 27-Jun-2020 09:36:40

290 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar calendar = new GregorianCalendar();To get the day of the week, use the following field.GregorianCalendar.DAY_OF_WEEKThe following is an example.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar calendar = new GregorianCalendar();       System.out.println("Day of Week = " + calendar.get(GregorianCalendar.DAY_OF_WEEK));       System.out.println("Date = " + calendar.get(GregorianCalendar.DATE));       System.out.println("Month = " + calendar.get(GregorianCalendar.MONTH));       System.out.println("Year = " + calendar.get(GregorianCalendar.YEAR));    } }OutputDay of Week = 2 Date = 19 Month = 10 Year = ... Read More

Set a GregorianCalendar object to a particular date in Java

karthikeya Boyini
Updated on 27-Jun-2020 09:37:21

302 Views

To work with the GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Firstly, create a GregorianCalendar object.GregorianCalendar calendar = new GregorianCalendar();Now, set the above-created object to a date. Here 0 is for 1st month.calendar.set(2018, 0, 25);The following is an example.Example Live Demoimport java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; public class Demo {    public static void main(String[] args) {       GregorianCalendar calendar = new GregorianCalendar();       // 0 is for 1st month       calendar.set(2018, 0, 25);       System.out.println("" + calendar.getTime());    } }OutputThu Jan 25 16:51:24 UTC 2018

Set the Date and Time with Gregorian Calendar in Java

Samual Sam
Updated on 27-Jun-2020 09:07:16

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

Specify the TimeZone explicitly for Gregorian Calendar in Java

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

146 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

Gregorian Calendar in Java

Samual Sam
Updated on 27-Jun-2020 09:13:00

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

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

Advertisements