Samual Sam has Published 2492 Articles

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

Samual Sam

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

Java Program to use == operator to compare enums

Samual Sam

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

Create BigInteger via string in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:12:13

107 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Firstly, set a string −String str = "268787878787687";Now, create a new object for ... Read More

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

Samual Sam

Samual Sam

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

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

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

Samual Sam

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

Get the days remaining in current year in Java

Samual Sam

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

Display first two digits of year in Java (two-digit century)

Samual Sam

Samual Sam

Updated on 27-Jun-2020 14:53:05

355 Views

Use the ‘C’ date conversion character to display two digits of year −System.out.printf("Two-digit Year (Century Name) = %tC/%TC", d, d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] ... Read More

Display two-digit year in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 14:51:59

1K+ Views

Use the ‘y’ date conversion character to display two-digit year.System.out.printf("Two-digit Year = %TY", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {     ... Read More

Display two-digit month in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 14:50:32

2K+ Views

Use the ‘m’ date conversion character to display two-digit month.System.out.printf("Two-digit month: %tm", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       ... Read More

Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

Samual Sam

Samual Sam

Updated on 27-Jun-2020 14:48:28

267 Views

The length of the longest consecutive 1’s in the binary representation of a given integer involves the length of the longest series of 1’s that occur together. An example of this is given as follows −Number = 13 Binary representation = 1101The length of the longest consecutive 1’s in binary ... Read More

Advertisements