Nilesh Verma
Nilesh Verma

Reputation: 924

how do i set custom date in android

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;
}

Upvotes: 4

Views: 4852

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338256

tl;dr

Use java.time in modern Java, and in Android too.

LocalDate xmasThisYear = 
    MonthDay                               // Represent a month and day-of-month, but no year.
    .of( Month.DECEMBER , 25 )             // Returns a `MonthDay` object. 
    .atYear (
        Year                               // Represent a year.
        .now( ZoneId.of( "Asia/Tokyo" ) )  // Get the current year as seen in the wall-clock/calendar of a particular time zone.
        .getValue()                        // Extract the number of the year, an `int` value.
    )                                      // Returns a `LocalDate` object representing a date, a year with month and day-of-month.
;

java.time

Never use the terribly-flawed legacy date-time classes such as Date, Calendar, Timestamp, etc.

Android 26+ comes with an implementation of the java.time classes that were first invented for Java 8+. For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring”.

MonthDay

To represent the idea of Christmas on December 25 every year, use MonthDay.

MonthDay xmas = MonthDay.of( Month.DECEMBER , 25 ) ;

LocalDate

For a particular Christmas date, we need a year to produce a LocalDate object.

LocalDate xmas2020 = xmas.atYear( 2020 ) ;

Time zone

To determine the current year, we must specify a time zone. For any given moment, the time and the date vary around the globe by time zone. Given that the date varies, the month may vary as well. And if month, so too year. At the moment New Year’s Eve turns into New Year's Day in Toulouse France, it is already “next year” in Tokyo Japan while simultaneously “last year” in Toledo Ohio US.

ZoneId z = ZoneId.of( "America/Edmonton" );

Year

Now we can get the current year with Year class.

Year currentYear = Year.now( z ) ;

Pass the year-number from that Year object to MonthDay#atYear like did above.

LocalDate thisYearXmas = xmas.atYear( currentYear.getValue() ) ;

Sane numbering

Note that, in contrast to the legacy classes, java.time uses sane numbering. So months January-December are numbered 1-12 rather than 0-11.

Upvotes: 1

Uday
Uday

Reputation: 6023

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

Upvotes: 1

Caner
Caner

Reputation: 59158

Something like this should work:

 public static Calendar defaultCalendar() {
    Calendar currentDate = Calendar.getInstance();
    currentDate.set(currentDate.get(Calendar.YEAR),Calendar.DECEMBER,25);
    return currentDate;
}

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1499860

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;
}

Upvotes: 3

Related Questions