如何使用SimpleDateFormat显示当前日期?
我正在尝试格式化当前日期和时间,这是我的代码:
Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);
出了问题,因为我收到了这个字符串:19700103-0217
该日期不正确,我应该得到类似的内容这个:20111118-1217
如何解决这个问题?
i'm trying to get the current date and time formatted, this is my code:
Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);
Something is going wrong because i'm getting this string: 19700103-0217
That date is incorrect, I should get something like this: 20111118-1217
How to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是您的日期不正确。很难说它是如何发生的,因为你还没有表明它来自哪里,但这肯定是问题所在。要确认这一点,请使用内置的
Date.toString()
方法 - 该方法使用系统默认时区,因此您需要小心,但您至少应该看到正确的日期等。The problem is that your date is incorrect. It's hard to say how, as you haven't shown where it's coming from, but that's pretty much bound to be the problem. To confirm this, use the built-in
Date.toString()
method - that uses the system default time zone, so you need to be careful, but you should at least see the right date etc.我想说的是,您执行代码的设备(或模拟器)的日期不正确。您的代码应该按预期工作。
无论如何,它与
SimpleDateFormat
无关,只要您使用它的方式是正确的。I would say the device (or emulator) on which you execute your code has an incorrect date. Your code should work as expected.
In any case, it's not related to
SimpleDateFormat
, ythe way you use it is correct.如果您的目标只是显示当前日期,那么您可以使用
日历
。使用日历,很容易获得所需格式的日期。
例如:
有关更多甲酸盐,请参阅此。
If your goal is just to show current date then you can use
Calendar
though.Using calendar,its pretty easy to get the date in desired format.
Ex:
For more formates,refer this.
获取当前日期。试试这个
To get Current Date. Try this
//这将为您提供当前月份。
日期 date = calendar.getTime();
SimpleDateFormat 格式 = new SimpleDateFormat("MMM");
//这将为您提供该月的当前日期。
SimpleDateFormat format1 = new SimpleDateFormat("dd");
字符串 currentDate = format1.format(date);
如果您需要其他格式,可以看这个链接:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
//This will get you the current month.
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("MMM");
//This will get you the current date of the month.
SimpleDateFormat format1 = new SimpleDateFormat("dd");
String currentDate = format1.format(date);
If you need other formats, you can see this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
好吧,我可以帮助你,但我有自己的代码。如果你没有得到什么就问我。
首先,你必须创建一个任意名称的类,例如 date.class 并编写代码:
创建类后,你必须声明这一点:
现在找到要在其中显示日期的编辑框,如下所示:
现在你有在编辑框中显示日期如下:
我希望你得到我的答案。
Well, I can help you out but I have my own code. Ask me if you don't get something.
First of all you have to make a class of any name like date.class and write the code:
After making the class, you have to declare this:
Now find the edit box in which you want to display the date like:
Now you have to display date on edit box as follows:
I hope you got my answer.