androiddatecalendargettime

Android: Date (year,month,day)


I want to get the date as a year, month ,day without hours or minutes or any thing else, and I don't want to get the year alone and the month and the day each by its self. Because as a full date I need it to comparison with another date

such as today 28.11.2012 and to compare it to 11.12.2011 as if today minus 11.12.2011 more than 280 day I want to execute some code


Solution

  • you can use SimpleDateFormat.

    The basics for getting the current date

    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());
    

    EDITED : First of All you have the date in String Formate. you have to Convert into date Formate. try below code to do that. you have apply same for both the String strThatDay & strTodaDay you will get Calender Object for both.

    String strThatDay = "2012/11/27";
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
      Date d = null;
      try {
       d = formatter.parse(strThatDay);//catch exception
      } catch (ParseException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } 
    
      Calendar thatDay = Calendar.getInstance();
      thatDay.setTime(d);
    

    after that try below code to get Day from two Date :

    long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
    
    long days = diff / (24 * 60 * 60 * 1000);
    

    try it out. Hope it will help you.