Search code examples
javaandroidandroid-calendar

How to change only month, year and date in Calendar object without changing the hours, mins and seconds?


I want to set now to have the current time fields (hours, minutes and seconds) but I want to set the year to 1970 January 1st. The code below doesnt do that using the set(int year, int month, int date) command. The Year, Month and date remains on the current (i.e. today's) date.

Calendar now = Calendar.getInstance();
now.set(1970, Calendar.JANUARY, 01);

How to change the year, month and date field only?


Solution

  • Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.YEAR, 1970);