Found 9150 Articles for Object Oriented Programming

Creating a string from a subset of the array elements in Java

Samual Sam
Updated on 27-Jun-2020 05:48:09

515 Views

To get a string from a subset of the character array elements, use the copyValueOf() method. This method returns a String that represents the character sequence in the array specified.Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Now, let us create a string from the subset of the above array elements.String str = String.copyValueOf(ch, 4, 2);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = String.copyValueOf(ch, 4, 2);       System.out.println(str);    } }OutputIN

Creating String Object from certain part of a character Array in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:49:54

149 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.String str = new String(ch, 4, 2);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = new String(ch, 4, 2);       System.out.println(str);    } }OutputIN

Creating String Object from Character Array in Java

Samual Sam
Updated on 27-Jun-2020 05:50:37

605 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T'};To create string object from the above character array is quite easy. Add the array to the string parameter as shown below −String str = new String(ch);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T'};       String str = new String(ch);       System.out.println(str);    } }OutputTEST

Argument Index in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:51:16

608 Views

Argument indices allow programmers to reorder the output. Let us see an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.printf("Before reordering = %s %s %s %s %s %s", "one", "two", "three", "four", "five", "six" );       System.out.printf("After reordering = %6$s %5$s %4$s %3$s %2$s %1$s", "one", "two", "three", "four", "five", "six" );       System.out.printf("Before reordering = %d %d %d", 100, 200, 300);       System.out.printf("After reordering = %2$d %3$d %1$d", 100, 200, 300);    } }OutputBefore reordering = one two three four five six After reordering = ... Read More

Display localized month name with printf method in Java

Samual Sam
Updated on 27-Jun-2020 05:35:41

165 Views

To display localized method name in Java, use the ‘B’ conversion character.System.out.printf("Localized month : %TB", d);To display method name in lowercase, use the “%tb”System.out.printf("Localized month : %tB", d);Example Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Morning/afternoon indicator: %tp", d);       System.out.printf("Morning/afternoon indicator: %Tp", d);       System.out.printf("Localized month : %tB", d);       System.out.printf("Localized month : %TB", d);    } }OutputMorning/afternoon indicator: pm Morning/afternoon indicator: PM Localized month : November Localized month : NOVEMBERRight justify and left justify values ... Read More

Locale-specific morning/afternoon indicator in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:36:27

122 Views

Locale-specific morning/afternoon indicator is the AM/PM marker indicator.Use the ‘p’ conversion character to display AM/PM.System.out.printf("Morning/afternoon indicator: %tp",d);Example Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Morning/afternoon indicator: %tp",d);       System.out.printf("Morning/afternoon indicator: %Tp",d);    } }OutputMorning/afternoon indicator: pm Morning/afternoon indicator: PM

Displaymilliseconds since the epoch with Java Date and Time Conversion Character

Samual Sam
Updated on 27-Jun-2020 05:37:12

196 Views

To display milliseconds since the epoch, use the ‘Q’ Date and Time conversion specifier.System.out.printf("Milliseconds since epoch = %TQ", d);The above would display milliseconds since.1970-01-01 00:00:00 GMTExample Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Nanoseconds = %tN", d);       System.out.printf("Seconds since epoch = %ts", d);       System.out.printf("Milliseconds since epoch = %TQ", d);    } }OutputNanoseconds = 050000000 Seconds since epoch = 1543241478 Milliseconds since epoch = 1543241478050

Display seconds since the epoch with Java Date and Time Conversion Character

karthikeya Boyini
Updated on 27-Jun-2020 05:38:01

445 Views

To display seconds since the epoch, use the ‘s’ Date and Time conversion specifier.System.out.printf("Seconds since epoch = %ts", d);The above would display seconds since.1970-01-01 00:00:00 GMTExample Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Nanoseconds = %tN", d);       System.out.printf("Seconds since epoch = %ts", d);    } }OutputNanoseconds = 364000000 Seconds since epoch = 1543241402

Display nanoseconds with Java Date and Time Conversion Character

Samual Sam
Updated on 27-Jun-2020 05:39:41

1K+ Views

To display nanoseconds, use the ‘N’ Date and Time conversion specifier.System.out.printf("Nanoseconds = %tN", d);Example Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Nanoseconds = %tN", d);    } }OutputNanoseconds = 092000000

Display numbers with thousands separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:41:38

6K+ Views

To display number with thousands separator, set a comma flag.System.out.printf( "%,d",78567);The above would result.78, 567Let’s check for bigger numbers.System.out.printf( "%,d", 463758);The above would result.463,758Example Live Demopublic class Demo {    public static void main( String args[] ) {       System.out.printf( "%,d", 95647 );       System.out.printf( "%,d", 687467 );       System.out.printf( "%,.2f", 7546.21 );       System.out.printf( "%,.2f", 463758.787 );       System.out.printf( "%,.2f", 123456.5 );    } }Output95,647 687,467 7,546.21 463,758.79 123,456.50

Advertisements