Karthikeya Boyini has Published 2193 Articles

Compare date time using after() method of Java Calendar

karthikeya Boyini

karthikeya Boyini

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

289 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

216 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

182 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

576 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

163 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

450 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

Java Program to display time in 24-hour format

karthikeya Boyini

karthikeya Boyini

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

4K+ Views

Use the SimpleDateFormat class to display time in 24-hour format.Set the formatDate dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("kk:mm:ss");Now, the following will display time in 24-hour formatdateFormat.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 ... Read More

Format date with DateFormat.SHORT in Java

karthikeya Boyini

karthikeya Boyini

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

4K+ Views

DateFormat.SHORT is a constant for short style pattern.Firstly, we will create date objectDate dt = new Date(); DateFormat dateFormat;Let us format date for different locale with DateFormat.SHORT// FRENCH dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH); // GERMANY dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public ... Read More

Format time with DateFormat.SHORT in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 12:58:35

561 Views

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.SHORT is a constant for short style pattern.Firstly, we will create Date objectDate dt = new Date(); DateFormat dateFormat;Let us format time for different locale with DateFormat.SHORTdateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Locale CHINESE = ... Read More

Advertisements