![]() ![]() Writer.close() The output file ( datetime.txt) would have the following content: Now is 23:25:11 Writer.format("Today is %tD %n", new Date()) Writer.format("Today is %tF %n", new Date()) Writer.format("Now is %tT %n", new Date()) Max value of an int: 2147483647 (7fffffff) Java String Format Example #4:The following code uses the t prefix with date time conversions ( T, F and D) to write formatted Strings of current time and date to a text file: PrintWriter writer = new PrintWriter(new File("datetime.txt")) ("Max value of an int: %d (%1$x)%n", Integer.MAX_VALUE) Note that there are two format specifiers but only one argument so we use the argument index 1$ to specify the argument position for the specifier. ("Min value of an int: %d (%1$x)%n", Integer.MIN_VALUE) ("Max value of a byte: %d (%1$#x)%n", Byte.MAX_VALUE) Java String Format Example #3:The following example prints numbers in both decimal format ( %d) and hexadecimal format ( %x and %#x): ("Min value of a byte: %d (%1$#x)%n", Byte.MIN_VALUE) Java String Format Example #2:The following example uses the flag ‘ -‘ to left-justify the Strings with the width of 30 characters ( %-30s): List listBook = Arrays.asList( It is sold at %.2f USD today.%n", title, price) Note that the %n format specified at the end of the String produces a line terminator specific to the underlying operating system.Output: Effective Java is a great book. Java String Format Example #1:The following code formats a String, an integer number and a float number with the precision of 2 numbers after the decimal point (. ![]() Now, let’s see various code examples that demonstrate the usages of format String syntax to format output.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |