Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.Date;
import java.util.Formatter;

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        Formatter fm = new Formatter(sb);

        // Formatting strings
        fm.format("%1$s, %2$s,  and  %3$s %n", "A", "B", "C");
        fm.format("%3$s, %2$s,  and  %1$s %n", "D", "E", "F");

        // Formatting numbers
        fm.format("%1$4d, %2$4d, %3$4d %n", 1, 10, 100);
        fm.format("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000);
        fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 1, 10, 100);
        fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 10, 100, 1000);

        // Formatting date and time
        Date dt = new Date();
        fm.format("Today  is %tD  %n", dt);
        fm.format("Today  is %tF %n", dt);
        fm.format("Today  is %tc %n", dt);

        // Display the entire formatted string
        System.out.println(sb.toString());

    }
}