Karthikeya Boyini has Published 2383 Articles

Java Program to construct one String from another

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:44:55

412 Views

To construct on string from another, firstly take a charcter array for the first string.char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch);The above forms first string. Now, let us created another string from the first string.String str2 = new String(str1);In this way, we can ... Read More

Display numbers with thousands separator in Java

karthikeya Boyini

karthikeya Boyini

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

4K+ 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 );   ... Read More

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

karthikeya Boyini

karthikeya Boyini

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

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

Locale-specific morning/afternoon indicator in Java

karthikeya Boyini

karthikeya Boyini

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

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

Unix date format in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:17:18

386 Views

Use the ‘c’ date conversion character to display UNIX date format in Java.System.out.printf("Unix date format: %tc", 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 d ... Read More

Floating-point conversion characters in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:14:59

278 Views

Floating-point conversion characters include the following.CharacterDescription%edecimal number in computerized scientific notation%Edecimal number in computerized scientific notation%fdecimal number%gbased on computerized scientific notation or decimal format, %Gbased on computerized scientific notation or decimal format, Example Live Demopublic class Demo {    public static void main(String[] args) throws Exception {       System.out.printf("Integer ... Read More

Copy characters from string into char Array in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:50:47

850 Views

Let’s say we have the following string.String str = "Demo Text!";To copy some part of the above string, use the getChars() method.// copy characters from string into chArray =str.getChars( 0, 5, chArray, 0 );The following is an example to copy characters from string into char Array in Java.Example Live Demopublic class ... Read More

Convert Integer to Hex String in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:34:42

12K+ Views

The Integer.toHexString() method in Java converts Integer to hex string.Let’s say the following are our integer values.int val1 = 5; int val2 = 7; int val3 = 13;Convert the above int values to hex string.Integer.toHexString(val1); Integer.toHexString(val2); Integer.toHexString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {     ... Read More

Java Program to convert int to Octal String

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:32:50

573 Views

The Integer.toOctalString() method in Java converts int to octal string.Let’s say the following are our integer values.int val1 = 9; int val2 = 20; int val3 = 2;Convert the above int values to octal string.Integer.toOctalString(val1); Integer.toOctalString(val2); Integer.toOctalString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {     ... Read More

Convert a String to a short number in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:21:34

195 Views

Use the Short.parseShort() method in Java to convert a String to a short.Let’s say the following is our string.String str = “76”;Now, the parseShort() method will convert the string to short.byte val = Byte.parseByte(str);Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "76";        short val = Short.parseShort(str);        System.out.println(val); ... Read More

Advertisements