java时间格式化错误_java – SimpleDateFormat显示错误的分钟,秒和毫秒

我已经编写了这个示例程序,我希望将日期转换为另一种格式.使用简单的日期格式时,我看不到预期的日期.

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

}

}

}

我看到以下输出.第一个逻辑是使用简单的日期格式,第二个逻辑是手动翻译.

Using SDF 2015-04-09 17:30:05.000862

Manual Translation 2015-04-09 17:18:48.677862000

那么如何使简单的日期格式输出与手动值完全相似的值

解决方法:

看起来您不能使用6位数的毫秒数.你的节目是11分17秒. 660秒= 11分钟,你有17秒的休息时间.所以它只是将您的输入转换为几分钟,因为它不能接受超过3毫秒的数字.

标签:java,datetime,simpledateformat

来源: https://codeday.me/bug/20191003/1848994.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值