String formatting in Java for Date class

I have this program to print the data in desired format. but I am having difficulty to get it working.

here is my code and output:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TestingStuff {

    public static void main(String[] args){
        Calendar calendar = Calendar.getInstance();
        calendar = new GregorianCalendar(2014, 07, 18);
        Date startDate = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
        String formatted = simpleDateFormat.format(startDate);
        String result="";
        String header2=String.format("%s %20s %20s %20s%n", "Account","Date","Hours","Skill");
        result+=header2;
        result+="-----------------------------------------------------------------------\n";
        result+=String.format("summary %20s%n",formatted);
        result+=String.format("Totabl Billable: %25s%n", 40);
        result+=String.format("Total Non-Billable: %25s%n", 0);
        result+=String.format("Total Hours %25s%n", 40);
        System.out.println(result);

    }

}

Output:

Account                 Date                Hours                Skill
-----------------------------------------------------------------------
summary             08/18/14
Totabl Billable:                        40
Total Non-Billable:                         0
Total Hours                        40

Expected output:

    Account                 Date                Hours                Skill
    -----------------------------------------------------------------------
    summary                08/18/14
    Totabl Billable:                              40
    Total Non-Billable:                            0
    Total Hours                                   40

As you can see the numbers are not centered with respect to tab headers. I am also hard coding the number of spaces such as (%20s) which I guess is not the right way. Is there a way to tell to format automatically with centering?

Answers


I would recommend using the java.util.Formatter '-' flag for Left Justify. From the Javadoc:

'-' '\u002d' Left justifies the output. Spaces ('\u0020') will be added at the end of the converted value as required to fill the minimum width of the field.

So, if we apply this to your example:

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar = new GregorianCalendar(2014, 07, 18);
    Date startDate = calendar.getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
    String formatted = simpleDateFormat.format(startDate);
    String result = "";
    String header2 = String.format("%s %20s %20s %20s%n", "Account", "Date", "Hours", "Skill");
    result += header2;
    result += "-----------------------------------------------------------------------\n";
    result += String.format("summary %20s%n", formatted);
    result += String.format("%-40s %4d%n", "Totabl Billable:", 40);
    result += String.format("%-40s %4d%n", "Total Non-Billable:", 0);
    result += String.format("%-40s %4d%n", "Total Hours:", 40);
    System.out.println(result);
}

Will pad the first input (the row label) with spaces to match a fixed width (40 in this case). This will produce:

Account                 Date                Hours                Skill
-----------------------------------------------------------------------
summary             08/18/14
Totabl Billable:                           40
Total Non-Billable:                         0
Total Hours                                40

This gives the added benefit of working for dynamic row labels since it automatically pads to the specified width regardless of the length of the label. It will be more flexible then trying to calculate the space for every possible label.


        String header2=String.format("%s\t\t\t%s\t\t\t%s\t\t\t%s\n", "Account","Date","Hours","Skill");
        result+=header2;
        result+="-------------------------------------------------------------------------------\n";
        result+=String.format("summary \t\t%s\n",formatted);
        result+=String.format("Totabl Billable: \t\t\t\t%s\n", 40);
        result+=String.format("Total Non-Billable: \t\t\t\t%s\n", 0);
        result+=String.format("Total Hours \t\t\t\t\t%s\n", 40);
        System.out.println(result);

this shoud work.Worked for me even when i put different numbers.


Need Your Help

update table using join codeigniter active record?

php mysql codeigniter

here is a snippet of my code using active record to update the item_stock values.

(GNU inline assembly) How to use a register which not assigned from nor copy to the C variables?

c gnu inline-assembly

I'm writing inline assembly statements using a GNU-based toolchain, and there are three instructions within the inline assembly to update a single bit of a system register. The steps will be: