3

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

I am using this code

public static Calendar defaultCalendar() {
    Calendar currentDate = Calendar.getInstance();
    currentDate.add(Calendar.YEAR,0);
    currentDate.add(Calendar.MONTH, 12);
    currentDate.add(Calendar.DATE,25);
    return currentDate;
}
anujprashar
  • 6,092
  • 6
  • 44
  • 79
Nilesh Verma
  • 894
  • 1
  • 11
  • 26

3 Answers3

5

Something like this should work:

 public static Calendar defaultCalendar() {
    Calendar currentDate = Calendar.getInstance();
    currentDate.set(currentDate.get(Calendar.YEAR),Calendar.DECEMBER,25);
    return currentDate;
}
Caner
  • 49,709
  • 33
  • 153
  • 169
3

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;
}
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
1

Use this it found very usefull to me though :

Take a look at SimpleDateFormat.

The basics for getting the current time in ISO8601 format:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
String now = df.format(new Date());

For other formats:

DateFormat df = new SimpleDateFormat("MMM d, yyyy");
    String now = df.format(new Date());

or

DateFormat df = new SimpleDateFormat("MM/dd/yy");
String now = df.format(new Date());

EDit:

Check this link it will help you :

Specific date

Community
  • 1
  • 1
Udaykiran
  • 5,705
  • 9
  • 41
  • 75