In post are covered string formats for:

  • String.format() - int, float, date, character, string
  • System.out.printf() - date, time, string, number, lpad and rpad
  • java.text.MessageFormat
  • LocalDateTime.parse()
  • datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  • format sql insert
  • IllegalFormatConversionException: d != java.lang.Doub

Format string with String.format()

If you need to prepare special string format you can use method format of String class:

String output = String.format("%s ; %d ; %c ; %d", "Java", 35, 'd', 64.3);
System.out.println(output);

String insert = String.format("insert into %s set id = %s where id = %d;", "mytable", "myvalue", 1);
System.out.println(insert);

result:

Java ; 35 ; d ; 64.300000
insert into mytable set id = myvalue where id = 1;

Format string with System.out.printf

printf can be useful when you want to output some well formatted results in the console. The same example from the above. Since printf doesn't print new line you need to add it by \n or to add empty println:

System.out.printf("%s ; %d ; %c ; %f", "Java", 35, 'd', 64.3);
System.out.println();
System.out.printf("insert into %s set id= %s where id = %d;
", "mytable", "myvalue", 1);

result:

Java ; 35 ; d ; 64.300000
insert into mytable set id = myvalue where id = 1;

Format dates(LocalDateTime) in java string

Dates could need many different formats depending on the many factors. In this example following formats are covered. In this example is included Java 8 LocalDateTime which is an immutable date-time object:

2018-03-18T12:44:17
2018-03-18
Sunday
2018-03-18
03/18/18
077
12:44:17

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

String fullDate = "2018-03-18 12:44:17.0";
LocalDateTime datetime = LocalDateTime.parse(fullDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));
System.out.println("Full date:" + datetime);
String shortDate = datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("yyyy-mm-yy date:" + shortDate);
System.out.printf("Day of Week:  %tA \n", datetime);
System.out.printf("yyyy-mm-dd:  %tF \n", datetime);
System.out.printf("dd/mm/yy:  %tD \n", datetime);
System.out.printf("Day of Year:  %tj \n", datetime);
System.out.printf("Time:  %tT \n", datetime);

result:

Full date: 2018-03-18T12:44:17
short date: 2018-03-18
Day of Week: Sunday
yyyy-mm-dd: 2018-03-18
dd/mm/yy: 03/18/18
Day of Year: 077
Time: 12:44:17

Format numbers in java string

In java if you need to add extra spaces to number or string in order to create nice look ( sometimes refered as lpad or rpad function) you can do it by: %10d or %-10d (for left align). You can also add commas to number simply by: %,d

System.out.printf("%d \n", 2018); 
System.out.printf(";%-10d; \n", 2018);
System.out.printf(";%10d; \n", 2018); 
System.out.printf(";% d    \n", 2018);
System.out.printf(";%,d;    \n", 20180317);

result:

2018
;2018 ;
; 2018;
; 2018
;20,180,317;

Format strings in java with MessageFormat

Another way for string formatting in java is by using: import java.text.MessageFormat. For example if you need to prepare SQL select you can do it in this way:

int id = 1;
String table = "mytable";
Object[] strings = {new Long(id), table};
MessageFormat fmt = new MessageFormat("select * from  \"{1}\" where id = {0};");
System.out.println(fmt.format(strings));
System.out.println(MessageFormat.format("select * from  \"{1}\" where id = {0};", id, table));

result:

select * from "mytable" where id = 1;
select * from "mytable" where id = 1;

Error IllegalFormatConversionException

In case of wrong argument for a given type you will get this error:

String output = String.format("%d", 64.3);

raise:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double