Found 9326 Articles for Object Oriented Programming

Display three-digit day of the year in Java

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

347 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

593 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

351 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

141 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

Set width and precision to format output in Java

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

1K+ Views

Set width in output as shown below. Here, %10 sets a 10 character fieldSystem.out.printf("d1 = %10.2f d2 = %8g", d1, d2);Above, the value after the decimal is for decimal places i.e. 5.3 is for 3 decimal places −System.out.printf("d1 = %5.3f d2 = %2.5f", d1, d2);The following is an example −Example Live Demopublic class Demo { public static void main(String[] args) throws Exception { double d1 = 399.8877; double d2 = 298.87690; System.out.printf("d1 = %10.2f d2 = %8g", d1, d2); System.out.printf("d1 = %5.3f d2 = %2.5f", d1, d2); } }Outputd1 = 399.89 d2 = 298.877 d1 = 399.888 d2 = 298.87690

Format numerical data with printf in Java

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

87 Views

First, we have taken a double variable and displayed it twice.double d = 399.8877; System.out.printf("d1 = %2$f d2 = %1$g", d, d);After that, we have formatted numerical int data −int val1 = 90, val2 = 35, val3 = 88, val4 = 600; System.out.printf("val1 = %d, val2 = %x, val3 = %o, val4 = %d", val1, val2, val3, val4); System.out.printf("val2 = %4$d, val2 = %3$d, val3 = %2$o, val4 = %1$d", val1, val2, val3, val4);Above, we have displayed %o for octal, %x for hexadecimal, %d for integer, etc.The following is the complete example −Example Live Demopublic class Demo {    public static ... Read More

Format Year in yyyy format in Java

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

284 Views

To format year in yyyy format is like displaying the entire year. For example, 2018.Use the yyyy format like this −SimpleDateFormat("yyyy");Let us see an example −// year in yyyy format SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy"); String strYear = simpleformat.format(new Date()); System.out.println("Current Year = "+strYear);Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time ... Read More

Format Year in yy format in Java

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

983 Views

To format year in yy format is like displaying year as 01, 02, 03, 04, etc. For example, 18 for 2018.Use the yy format like this.SimpleDateFormat("yy");Let us see an example −// year in yy format SimpleDateFormat simpleformat = new SimpleDateFormat("yy"); String strYear = simpleformat.format(new Date()); System.out.println("Current Year = "+strYear);Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current ... Read More

Advertisements