Getting Year, Month and Date in Android

前端 未结 9 1939
走了就别回头了
走了就别回头了 2021-02-06 21:04

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

Calendar calendar = Calendar.getInstance();

int thisYear = calendar.get(Calendar.YEAR);
L         


        
9条回答
  •  春和景丽
    2021-02-06 21:50

    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

提交回复
热议问题