在 Android 中获取年、月和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12233803/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:07:15  来源:igfitidea点击:

Getting Year, Month and Date in Android

androidcalendar

提问by Eranga Peris

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);

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

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

回答by James

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.

根据记忆,该代码应该按照您的预期执行。

回答by Sujewan

Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.get(Calendar.YEAR)

回答by Shalauddin Ahamad Shuza

The month starts from zero so you have to add 1 with the given month to show the month number we are familiar with.

月份从零开始,因此您必须在给定月份上加 1 以显示我们熟悉的月份数。

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

This will show you the current month starting with 1.

这将显示从 1 开始的当前月份。

回答by Moeed Ahmed

import java.util.Calendar;
import java.util.TimeZone;
import android.widget.Toast;

Calendar calendar = Calendar.getInstance(TimeZone.getDefault());

int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

Toast.makeText(this,"Today's Date: " + currentYear + currentMonth + currentDay, Toast.LENGTH_SHORT).show();

"TimeZone" will work great if your application targets for Android API 27 platform or above

如果您的应用程序面向 Android API 27 平台或更高版本,“TimeZone”将非常有用

回答by Ganesh Giri

try this one..

试试这个..

 Calendar instance = Calendar.getInstance();
 currentMonth = instance.get(Calendar.MONTH);
 currentYear = instance.get(Calendar.YEAR);

 int month=currentMonth+1;

回答by NinjAndroid

Try to use:

尝试使用:

Date dt = new Date(); 
dt.getYear(); 
dt.getMonth(); 
dt.getDay();

and see if you get the same result. If so, your system date is probably out of sync. Check the Date class documentationfor more details:

看看你是否得到相同的结果。如果是这样,您的系统日期可能不同步。查看Date 类文档以获取更多详细信息:

回答by AAnkit

I tried your code and it is giving correct output. You should try checking time in your emulator/phone on which you are trying this code.

我试过你的代码,它给出了正确的输出。您应该尝试在您尝试使用此代码的模拟器/手机中检查时间

According to getInstance docs, it sets to current date and time by Default.

根据 getInstance 文档,它默认设置为当前日期和时间。

回答by pyus13

This gives the time and date of your android system so first check it.

这给出了你的 android 系统的时间和日期,所以首先检查它。