Karthikeya Boyini has Published 2383 Articles

Comparing Enumeration Values in Java

karthikeya Boyini

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

Implement Switch on Enum in Java

karthikeya Boyini

karthikeya Boyini

Updated on 29-Jun-2020 05:15:19

159 Views

Enum in Java contains a fixed set of constants. They can have fields, constructors and method. It enhances type safety in Java.The following is an example wherein we are implementing Switch statement on Enumeration in Java −Example Live Demopublic class Demo {    public static void main(String[] args) {     ... Read More

Get the day of week for a particular date in Java

karthikeya Boyini

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

Days till End of Year in Java

karthikeya Boyini

karthikeya Boyini

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

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

Convert day of year to day of month in Java

karthikeya Boyini

karthikeya Boyini

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

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

Display four-digit year in Java

karthikeya Boyini

karthikeya Boyini

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

603 Views

Use the ‘Y’ date conversion character to display four-digit year.System.out.printf("Four-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 three-digit day of the year in Java

karthikeya Boyini

karthikeya Boyini

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

351 Views

Use the ‘j’ date conversion character to display three-digit day of the year.System.out.printf("Three-digit Day of the Year: %tj/%Tj", 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 day of the month in Java

karthikeya Boyini

karthikeya Boyini

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

663 Views

Use the ‘d’ date conversion character to display two-digit day of the month, for example, 27, 28, 20, etc.System.out.printf("Two-digit day of the month: %td", 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 { ... Read More

Java program to reverse bits of a positive integer number

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 14:47:41

215 Views

The bits of an integer number can be reversed to obtain another number. An example of this is given as follows −Number = 11 Binary representation = 1011 Reversed binary representation = 1101 Reversed number = 13A program that demonstrates this is given as follows −Example Live Demopublic class Example { ... Read More

Java Program to subtract 40 days from the calendar

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 13:50:31

255 Views

Firstly, you need to import the following package for Calendar class in Java −import 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 subtract 40 days using the calendar.add() method and Calendar.DATE constant. Set a ... Read More

Advertisements