Skip to content Skip to sidebar Skip to footer

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, '# thisYea

Solution 1:

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.

Solution 2:

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

Solution 3:

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

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

intcurrentYear= calendar.get(Calendar.YEAR);
intcurrentMonth= calendar.get(Calendar.MONTH) + 1;
intcurrentDay= 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

Solution 4:

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

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

This will show you the current month starting with 1.

Solution 5:

try this one..

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

 int month=currentMonth+1;

Post a Comment for "Getting Year, Month And Date In Android"