Found 9326 Articles for Object Oriented Programming

Add week to current date using Calendar.add() method in Java

Samual Sam
Updated on 27-Jun-2020 13:34:17

1K+ Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current date and timeCalendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant.calendar.add(Calendar.WEEK_OF_YEAR, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Adding 2 weeks       calendar.add(Calendar.WEEK_OF_YEAR, 2);       System.out.println("Updated Date = " + calendar.getTime());   ... Read More

Subtract seconds from current time using Calendar.add() method in Java

karthikeya Boyini
Updated on 27-Jun-2020 13:35:46

439 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current date and timeCalendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the seconds using the calendar.add() method and Calendar.SECOND constant. Set a negative value since we are decrementingcalendar.add(Calendar.SECOND, -20);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 20 seconds from current date       calendar.add(Calendar.SECOND, ... Read More

Java Program to add days to current date using Java Calendar

Samual Sam
Updated on 27-Jun-2020 13:39:54

762 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us add days using the add() method and Calendar.DATE constantcalendar.add(Calendar.DATE, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing Days by 2       calendar.add(Calendar.DATE, 2);       System.out.println("Updated Date = " + calendar.getTime());   } }OutputCurrent Date = ... Read More

Decrement a Month using the Calendar Class in Java

karthikeya Boyini
Updated on 27-Jun-2020 13:41:05

168 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementingcalendar.add(Calendar.MONTH, -2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing Month by 2       calendar.add(Calendar.MONTH, -2);       System.out.println("Updated ... Read More

Increment a Month using the Calendar Class in Java

Samual Sam
Updated on 27-Jun-2020 13:24:02

395 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the month using the add() method and Calendar.MONTH constant −calendar.add(Calendar.MONTH, 2);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing Month by 2       calendar.add(Calendar.MONTH, 2);       System.out.println("Updated Date (+2 Months) = " + calendar.getTime());   ... Read More

Java Program to decrement a Date using the Calendar Class

karthikeya Boyini
Updated on 27-Jun-2020 13:25:41

120 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the date using the add() method and Calendar.DATE constant. Set a negative value since we are decrementing the datecalendar.add(Calendar.DATE, -3);The following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing date by 3       calendar.add(Calendar.DATE, -3);       ... Read More

Increment a Date using the Java Calendar Class

Samual Sam
Updated on 27-Jun-2020 13:26:13

230 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us increment the date using the add() method and Calendar.DATE constantcalendar.add(Calendar.DATE, 2);The following is the complete exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Incrementing date by 2       calendar.add(Calendar.DATE, 2);       System.out.println("Updated Date = " + calendar.getTime());    } }OutputCurrent ... Read More

Compare date time using before() method of Java Calendar

karthikeya Boyini
Updated on 27-Jun-2020 13:27:44

126 Views

The Calendar.before() method returns whether this Calendar's time is before the time represented by the specified Object.First, let us set a date which is in the past (past date)Calendar beforeDate = Calendar.getInstance(); beforeDate.set(Calendar.YEAR, 2010); beforeDate.set(Calendar.MONTH, 05); beforeDate.set(Calendar.DATE, 30);Here is our date, which is 11-22-2018Calendar currentDate = Calendar.getInstance();Now, use the before() method to compare both the dates as shown in the following exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar beforeDate = Calendar.getInstance();       beforeDate.set(Calendar.YEAR, 2010);       beforeDate.set(Calendar.MONTH, 05);       beforeDate.set(Calendar.DATE, 30);       ... Read More

Compare date time using after() method of Java Calendar

karthikeya Boyini
Updated on 27-Jun-2020 13:29:04

186 Views

The Calendar.after() method returns whether this Calendar's time is after the time represented by the specified Object.First, let us set a date which is after (future date) the current dateCalendar afterDate = Calendar.getInstance(); afterDate.set(Calendar.YEAR, 2025); afterDate.set(Calendar.MONTH, 05); afterDate.set(Calendar.DATE, 30);Here is our date, which is 11-22-2018Calendar currentDate = Calendar.getInstance();Now, use the after() method to compare both the dates as shown in the following exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar afterDate = Calendar.getInstance();       afterDate.set(Calendar.YEAR, 2025);       afterDate.set(Calendar.MONTH, 05);       afterDate.set(Calendar.DATE, 30);     ... Read More

Java Program to format Month in MMMM format

Samual Sam
Updated on 27-Jun-2020 13:29:42

226 Views

Set the month format as MMMM, while including the date-time format in SimpleDateFormat object.Firstly, set the date objectDate dt = new Date();Now, set the format for date-timeSimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM dd yyyy kk:mm:ss");Display the date with the format you wantdateFormat.format(dt)The following is an exampleExample Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo {    public static void main(String[] argv) throws Exception {       Date dt = new Date();       SimpleDateFormat dateFormat;       dateFormat = new SimpleDateFormat("EEEE MMMM dd yyyy kk:mm:ss");       System.out.println(dateFormat.format(dt));    } }OutputThursday November 22 2018 11:45:14

Advertisements