在 Java (Android) 中以 YYYY-MM-DD 格式获取当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43983399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current date in YYYY-MM-DD format in Java (Android)
提问by petehallw
I am trying to get a Date
object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
我正在尝试以Date
表示当前系统日期的 YYYY-MM-DD 格式获取对象。以下是我的尝试:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
这些值如下:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
今天日期: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
今天字符串: 2017-24-15
Having tried this a couple of times I noticed the todayString
is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
尝试了几次后,我注意到todayString
它不是由 YYYY-MM-DD 组成,而是由 YYYY-[minutes][minutes]-DD 组成。
How might I get the current date in the YYYY-MM-DD format?
我如何获得 YYYY-MM-DD 格式的当前日期?
回答by Daniele
Change this:
改变这个:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
对此:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month
mm 代表分钟,而 MM(大写)代表月份