How to print a date with a custom format in Java 8?

Sirsemy :

I'd like to create a formatted chart as the one displayed below:

enter image description here

The character count in each of the fields is 4, 12, 17, 40 and 4 respectively. The fields are also left-justified.

By using the code that I posted below, my result looks like this:

Code snippet for formatting:

for (Tasks examined : shortList) {
  String yesNo = examined.isDone() == true ? "yes" : "no";
  System.out.printf("%1$-4s" + "| " + "%2$tY.%<tm.%<-12td." 
     + "| " + "%3$-17s" + "| " + "%4$-40s" + "| " + yesNo + "\n"
               , examined.getNumber(), examined.getDeadline()
               , importanceLevel, examined.getName())
}

The variable types are:

examined.getNumber() //integer
examined.getDeadline() //DateTime
importanceLevel //String
examined.getName() //String

I've studied the Formatter class in the JavaDocs here. But I have couldn't solve the problem.

The main issue here, is the dot (.) at the end of the 'date' field. It shouldn't have all that whitespace between the date and the full-stop.

I wanted to organize the width of the 'date' element, with the -12 flag, but that just cut the rest as the final dot above.

How could this be solved?

Soutzikevich :

This might solve your problem, if I understood your requirements correctly:

int index = 1;
                                   //yyyy, MM, dd, hh, min
LocalDateTime ldt = LocalDateTime.of(2018, 12, 31, 12, 10);
                               //specify custom pattern here
String date = DateTimeFormatter.ofPattern("yyyy.MM.dd.").format(ldt);
String importance = "kozepesen fontos";
String name = "egyik feladat";
String yesNo = "yes";
String formatter = "%-4d| %-12s| %-17s| %-40s| %-5s%n";
//now use System.out.format() to print with pattern specified in 'formatter'
System.out.format(formatter, index, date, importance, name, yesNo);

Output: 1 | 2018.12.31. | kozepesen fontos | egyik feladat | yes

  1. In the solution above, we inevitably convert the date to a string. That is, so we can specify a custom pattern for displaying the date. First we create a DateTimeFormatter() instance, with the desired pattern. The pattern is specified with the DateTimeFormatter's ofPattern(String pattern) method. Then, to get a String as the return type, we use the format(TemporalAccessor temporal) method from DateTimeFormatter again. We can use the DateTime instance as the TemporalAccessor parameter in format(), just as shown in the code above.

  2. So how does String formatter = "%-4d| %-12s| %-17s| %-40s| %5s%n" work? I used the syntax for the Formatter documentation for Java 8.

    - -> The result will be left justified.

    % -> The result is a literal.

    n -> The result is the platform-specific line separator.

    d -> The result is formatted as a decimal integer.

    s -> If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo() is invoked. Otherwise, the result is obtained by invoking arg.toString().

    Lastly, the numbers I used, specify the amount of characters to allow for each field.

  3. Then finally, we call System.out.format instead of printf(), where the format-specifying string is the first parameter and all the other variables to format must be typed in the correct order (the same order as the one in the format-specifying string).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=92792&siteId=1