在Android中获取年,月和日期 [英] Getting Year, Month and Date in Android

查看:365
本文介绍了在Android中获取年,月和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码获取今天的年,月和日期:

I am trying to get today's Year, Month and Date using following code;

Calendar calendar = Calendar.getInstance();

int thisYear = calendar.get(Calendar.YEAR);
Log.d(TAG, "# thisYear : " + thisYear);

int thisMonth = calendar.get(Calendar.MONTH);
Log.d(TAG, "@ thisMonth : " + thisMonth);

int thisDay = calendar.get(Calendar.DAY_OF_MONTH);
Log.d(TAG, "$ thisDay : " + thisDay);

但是它给出了2012年1月1日和28日日期,这不是今天的日期。我做错了什么?

But it gives "2012 for year 1 for month and 28 for date" which is not today's date. What I have done wrong?

推荐答案

假设这是运行在模拟器上,我会正确吗?如果是这样,请正确设置仿真器日期,并且它应该是正确的。

Would I be correct in assuming this is running on a Emulator? If so, Set the emulator date correctly, and it should be correct.

从内存中,该代码应该按照你的期望。

From memory, that code should do what you expect.

这篇关于在Android中获取年,月和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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