如何以与String相同的格式获取日期 [英] How to get Date in the same format as String

查看:98
本文介绍了如何以与String相同的格式获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在添加到现有Date对象几个月后,以yyyy-mm-dd格式获取包含Date的Date对象。
使用 DateFormat 对象我已经尝试过这种方式,但它没有给出我想要的输出。如何在Java中获取此Date格式?
我的代码 -

I want to get the Date object which contains Date in the form yyyy-mm-dd after adding few months to existing Date object. Using DateFormat object I have tried this way but its not giving the output as I want. How can I get this Date format in Java? My code-

         Calendar c=Calendar.getInstance();

        DateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
            Date d=cal.getTime();
            c.add(Calendar.MONTH,10);
            Date newd1=c.getTime();
            String news=sdf.format(newd1);
            Date dnew=(Date)sdf.parse(news);

String新闻的格式为yyyy-mm-dd,但是当我在该String上使用解析时结果Date对象的格式为Thu Jan 21 00:14:00 IST 2016。如何使用上述代码中的String消息以yyyy-mm-dd的形式获取Date对象。

String news has the date of format yyyy-mm-dd but when I use parse on that String it results Date object which is of format "Thu Jan 21 00:14:00 IST 2016". How can I get the Date object in the form of "yyyy-mm-dd" using String news in the above code.

推荐答案

java.util.Date没有格式。它只是日期。

java.util.Date doesn't have format. Its just Date.

您可以使用SimpleDateFormat

You can make it print its values in any form you want, by using SimpleDateFormat

这篇关于如何以与String相同的格式获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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