Found 9318 Articles for Object Oriented Programming

Java program to reverse bits of a positive integer number

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

217 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 {    public static void main(String[] args) {       int num = 14;       int n = num;       int rev = 0;       while (num > 0) {          rev = 1;       }       ... Read More

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

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 representation of 13 = 2A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String strings[]) {       int num = 55;       int n = num;       int count = 0;       while (num!=0) {          num = (num & (num

Display two-digit day of the month in Java

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

666 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 {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year ... Read More

Display two-digit month in Java

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 {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d);   ... Read More

Display three-digit day of the year in Java

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

354 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[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);     ... Read More

Display two-digit year in Java

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 {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d); ... Read More

Display four-digit year in Java

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

610 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 {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY",d);    } }OutputCurrent date and time = 26/11/2018 11:56:26 AM Four-digit Year = 2018

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

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

360 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[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Localized day name = %tA/%TA", d, d); ... Read More

Get localized short day-in-week name in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

223 Views

Use the ‘a’ date conversion character to display short day-in-week.System.out.printf("Localized short day name = %ta/%Ta", 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[] args) throws Exception { Date d = new Date(); DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); String format = dateFormat.format(d); System.out.println("Current date and time = " + ... Read More

Get localized day name in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

143 Views

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 { Date d = new Date(); DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); String format = dateFormat.format(d); System.out.println("Current date and time = " + format); System.out.printf("Localized day name = %tA/%TA", d, d); } }OutputCurrent date and time = 26/11/2018 11:44:44 AM Localized day name = Monday/MONDAY

Advertisements