Found 7442 Articles for Java

Compare date time using before() method of Java Calendar

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

218 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

290 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

318 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

Java Program to display time in 24-hour format

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 {       Date dt = new Date();       SimpleDateFormat dateFormat;       dateFormat = new SimpleDateFormat("kk:mm:ss");       System.out.println("Time in 24 hr format = "+dateFormat.format(dt));    } }OutputTime in 24 hr format = 11:40:52

Java Program to list Short Weekday Names

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

451 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 following is an example −Example Live Demoimport java.text.DateFormatSymbols; public class Demo {    public static void main(String[] args) {       String[] days = new DateFormatSymbols().getShortWeekdays();       for (int i = 0; i < days.length; i++) {          String weekday = days[i];          System.out.println(weekday);       }    } }OutputSun Mon Tue Wed Thu Fri Sat

Java program to list short month names

Samual Sam
Updated on 28-Aug-2024 21:18:31

2K+ Views

In this article, we will learn to list up the short month names in Java. To do so we will be using DateFormatSymbols class from java.text package. java.text: This package offers classes and interfaces for managing text, dates, numbers, and messages in a way that is not dependent on any specific language. DateFormatSymbols is a class that helps you work with date and time information that can be adapted to different languages and regions. It includes things like the names of the months, the days of the week, and details about time zones. Problem Statement Write a Java program that lists ... Read More

Change date formatting symbols in Java

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 an example −Example Live Demoimport java.text.SimpleDateFormat; import java.text.DateFormat; import java.text.DateFormatSymbols; import java.util.Date; public class Demo {    public static void main(String[] args) {       String[] weekDays1 = { "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };       String[] weekDays2 = { "", "monday", "tuesday", "wednesday", "thursday", ... Read More

Java Program to format time using Custom Format

Samual Sam
Updated on 27-Jun-2020 13:20:01

222 Views

Firstly, set the time with SimpleDateFormat classFormat dateFormat = new SimpleDateFormat("h:m:s");Now, for custom format, let us fetch the hour, minute and second individuallyHour// hour dateFormat = new SimpleDateFormat("h"); String strHour = dateFormat.format(new Date()); System.out.println("Hour: "+strHour);Minute// minute dateFormat = new SimpleDateFormat("m"); String strMinute = dateFormat.format(new Date()); System.out.println("Minute: "+strMinute);Second// second dateFormat = new SimpleDateFormat("s"); String strSecond = dateFormat.format(new Date()); System.out.println("Second: "+strSecond);The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Demo {    public static void main(String[] argv) throws Exception {       Format dateFormat = new SimpleDateFormat("h:m:s");       String str = dateFormat.format(new Date());   ... Read More

Java Program to display date with day name in short format

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 Demo {    public static void main(String[] argv) throws Exception {       Format dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy");       String res = dateFormat.format(new Date());       System.out.println("Date = " + res);    } }OutputDate = Thu, 22/11/2018

Java Program to set date formats for different countries

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

452 Views

Firstly, set the locale −Locale[] strLocales = { US, UK, GERMANY};Now, let us set the date formats i.e. different constants −int[] constants = { SHORT, MEDIUM, LONG }; String[] str = { "SHORT", "MEDIUM", "LONG" };Loop through and get the different date formats for different countries −for (Locale country : strLocales) {    System.out.println(""+ country.getDisplayCountry() + ".....");    for (int i = 0; i < constants.length; i++) {       dateFormat = DateFormat.getDateInstance(constants[i], country);       System.out.println(str[i] + " constant = " + dateFormat.format(dt));    } }The following is an exampleExample Live Demoimport static java.text.DateFormat.*; import static java.util.Locale.*; import ... Read More

Advertisements