Samual Sam has Published 2310 Articles

Right pad a string in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:30:47

1K+ Views

To right pad a string, use the String.format and set the spaces.String.format("%1$-" + 20 + "s", "demotext"));If you add 30 above, it will display the next string after 30 spaces from the beginning.String.format("%1$-" + 30 + "s", "demotext")The following is an example.Example Live Demopublic class Demo {    public static void ... Read More

Java Program to format a string

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:21:49

212 Views

To format a string, use the String.format() method in Java. The following is an example that formats a string %s.Example Live Demopublic class Demo {    public static void main(String []args) {       String str = String.format("%s %s", "demo", "text");       System.out.print("String: "+str);    } }OutputString: demo ... Read More

Perform Bubble Sort on strings in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:18:25

2K+ Views

To perform Bubble Sort, try the below given code. In this each each pair of adjacent elements is compared and the elements are swapped if they are not in order.The following is an example.Example Live Demopublic class Demo {    public static void main(String []args) {       String str[] ... Read More

Creating String Object from Character Array in Java

Samual Sam

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

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

Samual Sam

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

Display nanoseconds with Java Date and Time Conversion Character

Samual Sam

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

Displaymilliseconds since the epoch with Java Date and Time Conversion Character

Samual Sam

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

Display localized month name with printf method in Java

Samual Sam

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

Display ISO 8601 standard date in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 05:19:00

844 Views

Use the ‘F’ date conversion character to display ISO 8601 standard date.System.out.printf("ISO 8601 standard date = %tF", d);Above, d is a date object.Date d = new Date();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 ... Read More

Integral conversion characters in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 05:16:34

152 Views

Intergral conversion characters include the following.CharacterDescription%dInteger%oOctal%xHexadecimal%XHexadecimalExample Live Demopublic class Demo {    public static void main(String[] args) throws Exception {       System.out.printf( "Integer: %d", 889 );       System.out.printf( "Negative Integer: %d", -78 );       System.out.printf( "Octal: %o", 677 );       System.out.printf( "Hexadecimal: %x", ... Read More

Advertisements