File: FormatBasic.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (38 lines) | stat: -rw-r--r-- 1,447 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.GregorianCalendar;

public class FormatBasic {
    public static void main(String... p) {
        Formatter f = new Formatter();

        f.format("String");
        f.format("String %20% %n");
        f.format("%% %s", "str");
        f.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d");
        f.format("e = %+10.4f", Math.E);
        f.format("Amount gained or lost since last statement: $ %(,.2f", -6217.58);
        f.format("Local time: %tT", Calendar.getInstance());
        f.format("Unable to open file '%1$s': %2$s", "food", "No such file or directory");
        f.format(
                "Duke's Birthday: %1$tm %1$te,%1$tY",
                new GregorianCalendar(1995, Calendar.MAY, 23));
        f.format("Duke's Birthday: %tm %<te,%<tY", new Date());
        f.format("Duke's Birthday: %2$tm %<te,%<tY (it's the %dth)", 123, new Date());

        String s = "%+s%";
        // :: error: (format.string.invalid)
        f.format(s, "illegal");
        // :: error: (format.string.invalid)
        f.format("%+s%", "illegal");
        // :: error: (format.string.invalid)
        f.format("Wrong < indexing: %1$tm %<te,%<$tY", new Date());
        // :: error: (format.string.invalid)
        f.format("%t", new Date());
        // :: error: (argument.type.incompatible)
        f.format("%Td", (int) 231);

        f.close();
    }
}