以 (yyyy-MM-dd) 格式解析字符串日期 [英] Parse String date in (yyyy-MM-dd) format

查看:47
本文介绍了以 (yyyy-MM-dd) 格式解析字符串日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2013-09-18"形式的字符串.我想将其转换为 java.util.Date.我正在这样做

I have a string in the form "2013-09-18". I want to convert it into a java.util.Date. I am doing this

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(currentDateText);

convertedCurrentDate 是 'Wed Sep 18 00:00:00 IST 2013'

The convertedCurrentDate is coming as 'Wed Sep 18 00:00:00 IST 2013'

我希望我的输出格式为2013-09-18"

I want my output in the form '2013-09-18'

小时、分钟和秒不应该出现,它应该是 yyyy-MM-dd 的形式.

Hour, minutes and seconds shouldnt come and it should be in the form yyyy-MM-dd.

推荐答案

您可能需要按如下方式format输出.

You may need to format the out put as follows.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse("2013-09-18");
String date=sdf.format(convertedCurrentDate );
System.out.println(date);

使用

String convertedCurrentDate =sdf.format(sdf.parse("2013-09-18"));

输出:

2013-09-18

这篇关于以 (yyyy-MM-dd) 格式解析字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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