Java 显示分钟、秒和毫秒的SimpleDateFormat错误
我编写了这个示例程序,希望将日期转换为另一种格式。使用简单日期格式时,我看不到预期的日期Java 显示分钟、秒和毫秒的SimpleDateFormat错误,java,datetime,simpledateformat,Java,Datetime,Simpledateformat,我编写了这个示例程序,希望将日期转换为另一种格式。使用简单日期格式时,我看不到预期的日期 public class TestDate { /** * @param args */ public static void main(String[] args) { SimpleDateFormat originalformat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
public class TestDate {
/**
* @param args
*/
public static void main(String[] args) {
SimpleDateFormat originalformat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
SimpleDateFormat targetformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try {
//Use Simple Date Format
Date date = originalformat.parse("2015-04-09-17.18.48.677862");
System.out.println("Using SDF "+targetformat.format(date));
//Use Manual Translation
String eventTime = "2015-04-09-17.18.48.677862";
StringBuffer timeBuffer = new StringBuffer();
for (int i = 0; i < eventTime.length(); i++) {
if (i == 10) {
timeBuffer.append(" ");
continue;
} else if (i == 13 || i == 16) {
timeBuffer.append(":");
continue;
} else {
timeBuffer.append(eventTime.charAt(i));
}
}
timeBuffer.append("000");
String transformedTime = timeBuffer.toString().trim();
System.out.println("Manual Translation "+transformedTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
因此,如何使简单的日期格式输出与手动格式完全相似的值值
677862
被解释为毫秒,而不是微秒。这是677秒862毫秒。秒部分为11分17秒,添加到17.18.48
后成为17.30.05
要使用
S
格式,毫秒需要3位数字,而不是6位。您需要将字符串截断为超过最后一个小数点的3位。看起来您不能在毫秒内使用6位数字。你的节目还有11分17秒。660秒=11分钟,您还有17秒的休息时间。因此,它只是将输入从秒转换为分钟,因为它不能接受超过3毫秒的数字 您已指定677862毫秒,即677秒。。。如果要指定毫秒值,它应该是3位数,而不是6位数。
Using SDF 2015-04-09 17:30:05.000862
Manual Translation 2015-04-09 17:18:48.677862000