Karthikeya Boyini has Published 2383 Articles

Decrement a Month using the Calendar Class in Java

karthikeya Boyini

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

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

karthikeya Boyini

karthikeya Boyini

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

443 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 ... Read More

Java Program to add year to current date using Calendar.add method

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:33:42

918 Views

Firstly, you need to import the following package for Calendar class in Javaimport java.util.Calendar;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 add year using the calendar.add() method and Calendar.YEAR constantcalendar.add(Calendar.YEAR, 20);The following is an ... Read More

Compare date time using after() method of Java Calendar

karthikeya Boyini

karthikeya Boyini

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

188 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 ... Read More

Compare date time using before() method of Java Calendar

karthikeya Boyini

karthikeya Boyini

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

128 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() ... Read More

Java Program to decrement a Date using the Calendar Class

karthikeya Boyini

karthikeya Boyini

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

121 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, ... Read More

Set Date patterns with SimpleDateFormat in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:22:33

436 Views

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved) for Date and Time in JavaReference − Oracle JavaLetterDate or Time ComponentPresentationExamplesGEra designatorTextADYYearYear1996; 96YWeek yearYear2009; 09MMonth in yearMonthJuly; Jul; 07WWeek in yearNumber27WWeek in monthNumber2DDay in yearNumber189DDay in monthNumber10FDay of week ... Read More

Java Program to display date with day name in short format

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:20:44

3K+ Views

Firstly, set the format with SimpleDateFormat classFormat dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy");Above, the “EEE” is set to display the name of the day i.e. Monday, Tuesday, Wednesday, etc.Now, to display the date −String res = dateFormat.format(new Date());The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class ... Read More

Change date formatting symbols in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:19:19

92 Views

For date formatting symbols, use the DateFormatSymbols class.Firstly, set weekdays using a string arrayString[] weekDays2 = { "", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };Now, set short week days withDateFormatSymbols mySymbols = new DateFormatSymbols(); mySymbols.setShortWeekdays(weekDays2);Get the date −dateFormat = new SimpleDateFormat("EEEE, dd MMM yyyy", mySymbols); System.out.println(dateFormat.format(new Date()));The following is ... Read More

Java Program to list Short Weekday Names

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:07:02

345 Views

To list short weekday names, use the getShortWeekdays() from the DateFormatSymbols class in Java.DateFormatSymbols is a class for encapsulating localizable date-time formatting data.Get short weekday names in an arrayString[] days = new DateFormatSymbols().getShortWeekdays();Display the weekdayfor (int i = 0; i < days.length; i++) { String weekday = days[i]; System.out.println(weekday); }The ... Read More

Advertisements