使用简单格式将日期转换为字符串 [英] Convert Date to String using simpledateformat

查看:130
本文介绍了使用简单格式将日期转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将日期转换为不同格式的字符串时,我遇到问题。日期:

I'm having issues when converting a date to string in a different format. The date:

lastDownloadDate>>Wed Feb 27 16:20:23 IST 2013
lastChangeDate>>Wed Feb 27 15:11:00 IST 2013

我想将此格式转换为其他格式: yyyy-mm-dd HH:mm:ss

I want to convert this format to another format: yyyy-mm-dd HH:mm:ss.

当我尝试转换它,我得到不同的结果使用:

When I try to convert it I'm getting different results using:

DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");


String lastDownloadTimeVal = outputFormat.format(lastDownloadDate);
System.out.println("lastDownloadTimeVal>>"+lastDownloadTimeVal);
String lastChangeTimeVal = outputFormat.format(lastChangeDate);
System.out.println("lastChangeTimeVal>>"+lastChangeTimeVal);

我收到错误的结果,该月份正在被分钟替换:

I'm getting the wrong result, the month is being replaced with minutes:

lastDownloadTimeVal>>2013-20-27 16:20:23
lastChangeTimeVal>>2013-11-27 15:11:00


推荐答案

yyyy- dd HH:mm:ss 这是错误的。这应该是 yyyy-MM-dd HH:mm:ss
请参阅 http://docs.oracle .com / javase / 6 / docs / api / java / text / SimpleDateFormat.html 有关将日期从一种格式转换到另一种格式的更多详细信息。

yyyy-mm-dd HH:mm:ss this is wrong. This should be yyyy-MM-dd HH:mm:ss. Refer this http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html for more details regarding converting date from one format to another.

这篇关于使用简单格式将日期转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆