Karthikeya Boyini has Published 2193 Articles

Implement Switch on Enum in Java

karthikeya Boyini

karthikeya Boyini

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

240 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

4K+ 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

551 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

628 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

789 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

462 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

862 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

314 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

Decrement a Month using the Calendar Class in Java

karthikeya Boyini

karthikeya Boyini

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

228 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 month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementingcalendar.add(Calendar.MONTH, -2);The ... Read More

Subtract seconds from current time using Calendar.add() method in Java

karthikeya Boyini

karthikeya Boyini

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

665 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, 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 decrement the seconds using the calendar.add() method and Calendar.SECOND constant. Set a negative value since we ... Read More

Advertisements