[英]Getting Year, Month and Date in 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);
But it gives "2012 for year 1 for month and 28 for date" which is not today's date.但它给出了“2012 年第 1 年的月份和 28 的日期”,这不是今天的日期。 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.根据记忆,该代码应该按照您的预期执行。
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.get(Calendar.YEAR)
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”将非常有用
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 开始的当前月份。
try this one..试试这个..
Calendar instance = Calendar.getInstance();
currentMonth = instance.get(Calendar.MONTH);
currentYear = instance.get(Calendar.YEAR);
int month=currentMonth+1;
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 文档,它默认设置为当前日期和时间。
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 documentation for more details:查看Date 类文档以获取更多详细信息:
这给出了你的 android 系统的时间和日期,所以首先检查它。
You can use SimpleDateFormat您可以使用SimpleDateFormat
Initialising as @SuppressLint("SimpleDateFormat") final SimpleDateFormat dateAndTime = new SimpleDateFormat("DD-MM-yy", Locale.getDefault());
初始化为@SuppressLint("SimpleDateFormat") final SimpleDateFormat dateAndTime = new SimpleDateFormat("DD-MM-yy", Locale.getDefault());
and String timeStamp = dateAndTime.format(new Date());
和String timeStamp = dateAndTime.format(new Date());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.