Found 34494 Articles for Programming

Java Program to use == operator to compare enums

Samual Sam
Updated on 29-Jun-2020 05:16:12

105 Views

We can use the == operator to compare enums in Java.Let’s say we have the following enum.enum Devices {    LAPTOP, MOBILE, TABLET; }Here are some of the objects and we have assigned some values as well −Devices d1, d2, d3; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET;Let us now see an example wherein we will compare them using == operator −Example Live Demopublic class Demo {    enum Devices {       LAPTOP, MOBILE, TABLET;    }    public static void main(String[] args) {       Devices d1, d2, d3;       d1 = Devices.LAPTOP; ... Read More

Comparing Enumeration Values in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:17:11

170 Views

To compare enumeration values, use the equals() method.Our Devices enum is having some objects with values assigned to them.Devices d1, d2, d3; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET;Let us compare them −if(d3.equals(Devices.TABLET)) System.out.println("Devices are same."); else System.out.println("Devices are different.");The following is an example −Example Live Demopublic class Demo {    enum Devices {       LAPTOP, MOBILE, TABLET;    }    public static void main(String[] args) {       Devices d1, d2, d3;       d1 = Devices.LAPTOP;       d2 = Devices.LAPTOP;       d3 = Devices.TABLET;       if(d1.equals(d2))   ... Read More

The equals and == operator for Enum data type in Java

Samual Sam
Updated on 29-Jun-2020 05:17:47

113 Views

We have the Devices Enum with four constants.enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; }We have created some objects and assigned them with the constants.Let us compare them with both equals() and ==. Firstly, begin with equals() −if(d1.equals(d2)) System.out.println("Devices are the same."); else System.out.println("Devices are different.");Now, let us move forward and check for ==if(d1 == d3) System.out.println("Devices are the same."); else System.out.println("Devices are different.");The following is the final example demonstrating both equals() and == operator −Example Live Demopublic class Demo {    enum Devices {       LAPTOP, MOBILE, TABLET, DESKTOP;    }    public static void main(String[] args) { ... Read More

Enum for days of week in Java

Samual Sam
Updated on 29-Jun-2020 05:19:04

2K+ Views

To set enum for days of the week, set them as constantsenum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }Now create objects and set the above constants −Days today = Days.Wednesday; Days holiday = Days.Sunday;The following is an example −Example Live Demopublic class Demo {    enum Days {       Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday    }    public static void main(String[] args) {       Days today = Days.Wednesday;       Days holiday = Days.Sunday;       System.out.println("Today = " + today);       System.out.println(holiday+ " is holiday");    } }OutputToday = Wednesday Sunday is holiday

Convert day of year to day of month in Java

karthikeya Boyini
Updated on 29-Jun-2020 04:41:41

477 Views

Firstly, set the day of year using DAY_OF_YEAR constant.Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2018); cal.set(Calendar.DAY_OF_YEAR, 320);Now, get the day of month −int res = cal.get(Calendar.DAY_OF_MONTH);The following is an example −Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       cal.set(Calendar.YEAR, 2018);       cal.set(Calendar.DAY_OF_YEAR, 320);       System.out.println("Date = " + cal.getTime());       int res = cal.get(Calendar.DAY_OF_MONTH);       System.out.println("Day of month = " + res);    } }OutputDate = Fri Nov 16 07:54:55 UTC 2018 Day of month = 16Read More

Get the days remaining in current year in Java

Samual Sam
Updated on 29-Jun-2020 04:44:06

2K+ Views

To get the days remaining in the current year, find the difference between the total days in the current year with the total number of days passed.First, calculate the day of year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = calOne.get(Calendar.YEAR); Calendar calTwo = new GregorianCalendar(year, 11, 31); int day = calTwo.get(Calendar.DAY_OF_YEAR); System.out.println("Days in current year: "+day);The following is the final example that finds the difference between the above two to get the days remaining in the current year.ExampleLive Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static ... Read More

Days till End of Year in Java

karthikeya Boyini
Updated on 29-Jun-2020 04:50:15

431 Views

To get the days until the end of the year, find the difference between the total days in the current year with the total number of passed.Firstly, calculate the day of the year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = calOne.get(Calendar.YEAR); Calendar calTwo = new GregorianCalendar(year, 11, 31); int day = calTwo.get(Calendar.DAY_OF_YEAR); System.out.println("Days in current year: "+day);Find the difference and you will get the days until the end of the year.The following is the complete example.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String ... Read More

Checks if two calendar objects represent the same local time in Java

Samual Sam
Updated on 29-Jun-2020 04:51:44

102 Views

Use the == operator to compare two calendar objects.Let us first create the first calendar object and set date −Calendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2040); date1.set(Calendar.MONTH, 10); date1.set(Calendar.DATE, 25); date1.set(Calendar.HOUR_OF_DAY, 11); date1.set(Calendar.MINUTE, 30); date1.set(Calendar.SECOND, 10);Now, the following is the second calendar object −Calendar date2 = Calendar.getInstance(); date2.set(Calendar.YEAR, 2040); date2.set(Calendar.MONTH, 10); date2.set(Calendar.DATE, 25); date2.set(Calendar.HOUR_OF_DAY, 11); date2.set(Calendar.MINUTE, 30); date2.set(Calendar.SECOND, 10);Let is now compare them using == and && operators −if(date1.get(Calendar.SECOND) == date2.get(Calendar.SECOND) && date1.get(Calendar.MINUTE) == date2.get(Calendar.MINUTE) && date1.get(Calendar.HOUR) == date2.get(Calendar.HOUR) && date1.get(Calendar.DAY_OF_YEAR) == date2.get(Calendar.DAY_OF_YEAR) && date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR) ) {    System.out.println("The local time for the calendar objects is same...");    } ... Read More

Get the day of week for a particular date in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:10:49

3K+ Views

To get the day of week for a particular date in Java, use the Calendar.DAY_OF_WEEK constant.Let us set a date first.Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);Since, we have used the Calendar as well as GregorianCalendar classes, therefore at first, import the following packages.import java.util.Calendar; import java.util.GregorianCalendar;Now, we will find the day of week.int day = one.get(Calendar.DAY_OF_WEEK);The following is an exampleExample Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);       int day = one.get(Calendar.DAY_OF_WEEK);       System.out.println(day);   ... Read More

Java Program to compare dates if a date is before another date

Samual Sam
Updated on 29-Jun-2020 05:11:29

159 Views

To compare dates if a date is before another date, use the Calendar.before() method.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 before the current dateCalendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2010); date1.set(Calendar.MONTH, 11); date1.set(Calendar.DATE, 20);Here is our current dateCalendar date = Calendar.getInstance();Now, use the after() method to compare both the dates as shown in the following exampleThe following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar date1 = Calendar.getInstance();       ... Read More

Advertisements