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

有帮助吗?

解决方案

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.

其他提示

Always use Simpledateformat(yyyy/mm/dd) for comparision..

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");                           
String currentDateandTime = sdf.format(new Date());

Use this currentDateandTime to compare with other date.

I think this may be a solution.U have to get instance of 2 calendar (1 for current date and another for compare date.

    Calendar cal1=Calendar.getInstance();   
   Date dt=null;                                                     
   try{
   dt = sdf.parse(currentDateandTime);
   cal1.setTime(dt);
   }catch (ParseException e){
     e.printStackTrace();
   }                                                
   int currentDaycmp= cal1.get(Calendar.DAY_OF_MONTH);
   int currentMonthcmp=cal1.get(Calendar.MONTH);
   int currentYearcmp=cal1.get(Calendar.YEAR);

    Calendar cal2=Calendar.getInstance();   
    Date dtend=null;                                                     
    try{
    dtend = sdf.parse(comparedate);
    cal2.setTime(dtend);
    } catch (ParseException e) {
    e.printStackTrace();
    }                                               
     int currentDayend= cal2.get(Calendar.DAY_OF_MONTH);
     int currentMonend=cal2.get(Calendar.MONTH);
     int currentyearend=cal2.get(Calendar.YEAR);

now find the difference

currentDaycmp-currentDayend(your condition)..then execute your block..

U try this..May be meet ur requirement..

You may want to use Joda-Time for this:

final DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");

LocalDate first = LocalDate.parse("28.11.2012", formatter);
// LocalDate first = new LocalDate(2012, 11, 28);
// LocalDate first = LocalDate.now();
LocalDate second = LocalDate.parse("11.12.2011", formatter);

int daysBetween = Days.daysBetween(first, second).getDays();

You should be aware of that daysBetween is a negative value if the second date is before the first like in this example.

For the given example daysBetween is -353.

You can use the compareTo method. Firstly, make sure that the two dates you are using have the same format. That is, if one is YYYY,DD,MM then the other would be the same.

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
Date firstDate = sdf.parse("2012-11-27");
System.out.println(sdf.format(firstDate));

And then you would do a firsDate.compareTo(SecondDate);

if firstDate.compareTo(SecondDate) < 280 {
...
}
 Calendar todayCalendar = new GregorianCalendar();
         Calendar pickedDateCalendar = new GregorianCalendar();

         todayCalendar.set(currentYear,currentMonth,currentDay); 
         pickedDateCalendar.set(birthDayDatePicker.getYear(),birthDayDatePicker.getMonth(),birthDayDatePicker.getDayOfMonth());

         System.out.println("Days= "+daysBetween(todayCalendar.getTime(),pickedDateCalendar.getTime()));

         int Days = daysBetween(todayCalendar.getTime(),pickedDateCalendar.getTime());
public int daysBetween(Date d1, Date d2){
         return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
         }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top