开发者

how do i set custom date in android

开发者 https://www.devze.com 2023-04-11 11:50 出处:网络
How do i set the date to 25 -12(december)- current year. eg. I am using this code public static Calendar defaultCalendar() {

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

I am using this code

public static Calendar defaultCalendar() {
    Calendar c开发者_开发百科urrentDate = Calendar.getInstance();
    currentDate.add(Calendar.YEAR,0);
    currentDate.add(Calendar.MONTH, 12);
    currentDate.add(Calendar.DATE,25);
    return currentDate;
}


Something like this should work:

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号