how do i set custom date in android

后端 未结 3 1964
无人共我
无人共我 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:30

    Something like this should work:

     public static Calendar defaultCalendar() {
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(currentDate.get(Calendar.YEAR),Calendar.DECEMBER,25);
        return currentDate;
    }
    
    0 讨论(0)
  • 2021-01-20 20:41

    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 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题