Question

I want to format my output to look like following

  Account                      Date        Hours  Skill
  ---------------------------  ----------  -----  --------------------
  Acme Industries              03/06/2006      8  Software Engineer

but I get this:

Account                     Date       Hours   Skill
---------------------------  ----------  -----  --------------------
Acme Industries             08/18/14   8       software Engineer

my code is below:

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 separator = "---------------------------  ----------  -----  --------------------\n";
        String result="";
        String header=String.format("%-27s %-10s %-7s %s%n", "Account","Date","Hours","Skill");

        String output=String.format("%-27s %-10s %-7s %s%n", "Acme Industries", 
                formatted,8,"Software Engineer");

        System.out.println(header+separator+output);
    }

}

I need some tips on how to approach this kind of formatting problems, something on the line of left and right padding...

Was it helpful?

Solution

I would use something like this

// get i dashes as a string.
private static String getDashes(int i) {
    StringBuilder sb = new StringBuilder();
    while (i > 0) {
        sb.append("-");
        --i;
    }
    return sb.toString();
}

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);

    // one format string
    String fmtString = "%-27s  %-10s  %5s  %s%n";
    // get the various dashes.
    String separator = String.format(fmtString, getDashes(26),
            getDashes(9), getDashes(5), getDashes(20));
    // format the header
    String header = String.format(fmtString, "Account", "Date", "Hours",
            "Skill");

    // format the body
    String output = String.format(fmtString, "Acme Industries", formatted,
            8, "software Engineer");

    // print
    System.out.println(header + separator + output);
}

Which outputs

Account                      Date        Hours  Skill
--------------------------   ---------   -----  --------------------
Acme Industries              08/18/14        8  software Engineer

OTHER TIPS

Wrong Formatter

The answer by jclozano is correct: You are using the wrong formatter symbols.

Time Zone

Another problem is that you fail to specify a time zone. If you deploy this app to computer or JVM set to another time zone, you'll mysteriously get different output.

Almost always best to specify a time zone rather than rely on default.

Use a proper time zone name, never the 3-letter codes. For Atlanta GA in US, perhaps "America/New_York".

Joda-Time

The java.util.Date, .Calendar, and java.text.SimpleTextFormat classes are notoriously troublesome. Avoid them.

Use either Joda-Time or the new java.time.* classes bundled in Java 8.

Unlike a java.util.Date object, a Joda-Time DateTime knows its time zone.

The formatter symbols in Joda-Time are similar to, but quite exactly the same as, java.text.SimpleText. Study the Joda-Time doc if you want something different that I used below.

Here is some code to generate your 10-character string using Joda-Time 2.3.

DateTimeZone timeZone = DateTimeZone.forID( "America/New_York" );
DateTime dateTime = new DateTime( 2014, DateTimeConstants.AUGUST, 18, 0, 0, 0, timeZone );
DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy");
String dateTimeString = formatter.print( dateTime );
System.out.println( "dateTimeString: " + dateTimeString );

When run…

dateTimeString: 08/18/2014

You are telling your code to use the short year format on this line

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");

Have you tried using

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");

or

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyyy");

Why don't you just use same format string for all:

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 format = "    %-27s %-10s %-7s %s%n";  // <================ LOOK HERE
    String header = String.format(format, "Account", "Date", "Hours",
            "Skill");
    String separator = String.format(format, "---------------------------",
            "----------", "-----", "--------------------");
    String output = String.format(format, "Acme Industries", formatted, 8,
            "software Engineer");

    System.out.println(header + separator + output);
}

It generates nice output:

    Account                     Date       Hours   Skill
    --------------------------- ---------- -----   --------------------
    Acme Industries             08/18/14   8       software Engineer
    String separator = "---------------------------  ----------  -----  --------------------\n";
    String result="";
    String header=String.format("%-27s  %-10s  %-5s  %s%n", "Account","Date","Hours","Skill");

    String output=String.format("%-27s  %-10s  %5s  %s%n", "Acme Industries",
            formatted,8,"software Engineer");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top