To extract a substring as an array of characters in Java, use the getChars() method.Let’s say the following is our string and character array.String str = "World is not enough!"; char[] chArr = new char[10];Now, use the getChars() method to extract a substring.str.getChars(13, 19, chArr, 0);The above substring is an array of characters which can be displayed as shown in the complete example below −Example Live Demopublic class Demo { public static void main(String[] args) { String str = "World is not enough!"; char[] chArr = new char[10]; str.getChars(13, 19, chArr, ... Read More
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 easily construct one string from another.Example Live Demopublic class Demo { public static void main(String[] args) { char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch); String str2 = new String(str1); String str3 ... Read More
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
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
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
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
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
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
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 d = new Date(); System.out.printf("Four-digit Year = %TY",d); System.out.printf("Two-digit Year = %ty",d); System.out.printf("ISO 8601 standard date = %tF", d); } }OutputFour-digit Year = 2018 Two-digit Year = 18 ISO 8601 standard date = 2018-11-26
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 = new Date(); System.out.printf("Unix date format: %tc",d); System.out.printf("Unix date format: %Tc",d); } }OutputUnix date format: Mon Nov 26 12:24:10 UTC 2018 Unix date format: MON NOV 26 12:24:10 UTC 2018
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP