简体   繁体   English

DateFormat不以所需格式给出日期

[英]DateFormat not giving date in the desired format

I want to get the current date in the following format yyyy_mm_dd 我想以以下格式yyyy_mm_dd获取当前日期

Am Using the below code 我正在使用以下代码

DateFormat date = new SimpleDateFormat("yyyy_mm_dd");
Date datetoday = new Date();
System.out.println(date.format(datetoday));

But its giving the result in this manner - 2013_56_25 但它以这种方式给出结果-2013_56_25

However, it should give the result like 2013_03_25 但是,其结果应为2013_03_25

Infact, the mm value varies every time I run it. 实际上,每次运行mm值都会有所不同。 My system's date is of the format - 3/25/2013. 我系统的日期格式为-3/25/2013。 (Idk if it has some relation with this) (Idk,如果与此有关系)

What is the possible problem with this ? 这可能是什么问题?

使用大写字母MM ,小写字母mm持续几分钟。

DateFormat date = new SimpleDateFormat("yyyy_MM_dd");

月份是M,而不是m(分钟)

Use MM instead of mm ; MM代替mm ; lower case is for mintues; 小写字母是小点心; Uppercase for Months 数月的大写

You will have to use MM and not mm. 您将必须使用MM而不是mm。 Please refer the table from the image below 请参考下图的表格

在此处输入图片说明

I got Different Result after parse the java.util.Date to java.sql.Date String date="2012-05-02"; 将java.util.Date解析为java.sql.Date String date =“ 2012-05-02”;之后,我得到了不同的结果。

SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd"); SimpleDateFormat df = new SimpleDateFormat(“ yyyy-mm-dd”);

i got as a result is:2012-01-01 我得到的结果是:2012-01-01

means 1 Day is less than actually required. 表示比实际需要的时间少1天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM