如何将String日期转换为Date? [英] How to convert String date to Date?

查看:114
本文介绍了如何将String日期转换为Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了以下代码:

I have done following code:

String str = "2013-01-17 11:26";
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:MM");
Date date = dateFormat.parse(str);
System.out.println("str :"+str);
System.out.println("Date :"+date);

输出

str :21-01-2013 11:26 
Date :Sat Feb 21 11:01:00 IST 2015

但有些事情是错误的。我期望与字符串日期一样。即Thu Jan 21 11:26:00 IST 2013

But something is wrong. I expect out as same as string date. i.e. Thu Jan 21 11:26:00 IST 2013

推荐答案

您使用的格式错误。 MM 用于 mm 用于分钟,而小时使用 HH 。请参阅 SimpleDateFormat 用于各种其他格式。

You are using the wrong format. MM is for month, and mm is for minutes, and for hours use HH. See SimpleDateFormat for various other formats.

将您的格式更改为: -

Change your format to: -

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

这篇关于如何将String日期转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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