我想将输出格式化为如下所示
Account Date Hours Skill
--------------------------- ---------- ----- --------------------
Acme Industries 03/06/2006 8 Software Engineer
但我明白了:
Account Date Hours Skill
--------------------------- ---------- ----- --------------------
Acme Industries 08/18/14 8 software Engineer
我的代码如下:
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);
}
}
我需要一些关于如何处理这种格式问题的提示,这些问题就在left and right padding
......
答案 0 :(得分:3)
我会用这样的东西
// 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);
}
哪个输出
Account Date Hours Skill
-------------------------- --------- ----- --------------------
Acme Industries 08/18/14 8 software Engineer
答案 1 :(得分:2)
answer by jclozano是正确的:您使用了错误的格式化符号。
另一个问题是您未能指定时区。如果您将此应用程序部署到计算机或JVM设置为另一个时区,您将神秘地获得不同的输出。
几乎总是最好指定时区而不是依赖默认值。
使用proper time zone name,而不是3个字母的代码。对于美国的亚特兰大GA,也许是“America / New_York”。
java.util.Date,.Calendar和java.text.SimpleTextFormat类非常麻烦。避免它们。
使用Joda-Time或Java 8中捆绑的新java.time。*类。
与java.util.Date对象不同,Joda-Time DateTime
知道它的时区。
Joda-Time中的格式化程序符号与java.text.SimpleText类似,但非常相同。如果你想要我在下面使用的不同的东西,请研究Joda-Time文档。
以下是使用Joda-Time 2.3生成10个字符的字符串的一些代码。
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 );
跑步时......
dateTimeString: 08/18/2014
答案 2 :(得分:1)
为什么不直接使用相同的格式字符串:
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);
}
它会产生很好的输出:
Account Date Hours Skill
--------------------------- ---------- ----- --------------------
Acme Industries 08/18/14 8 software Engineer
答案 3 :(得分:1)
您告诉您的代码在此行使用短年格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
您是否尝试过使用
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
或
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyyy");
答案 4 :(得分:0)
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");