how do i set custom date in android

后端 未结 3 1992
无人共我
无人共我 2021-01-20 20:19

How do i set the date to 25 -12(december)- current year. eg.

I am using this code

public static Calendar defaultCalendar() {
    Calendar currentDate         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 20:42

    You're trying to add 12 months, instead of setting the month to December (which is month 11, because the Java API is horrible). You want something like:

    public static Calendar defaultCalendar() {
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.MONTH, 11); // Months are 0-based!
        currentDate.set(Calendar.DAY_OF_MONTH, 25); // Clearer than DATE
        return currentDate;
    }
    

提交回复
热议问题