9

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

Android Developer
  • 1,019
  • 1
  • 9
  • 30
  • 1
    final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH);. This will give you current date,month and year – Raghunandan Nov 27 '12 at 07:56
  • I know this way but as I said, I don't want to get every thing separately – Android Developer Nov 27 '12 at 07:57
  • No as I dont want to know which is before or after the other – Android Developer Nov 27 '12 at 08:05
  • @Sam The question you posted is completely different from this question here. – Fabian Barney Nov 27 '12 at 09:20
  • @FabianBarney I disagree, the fundamental question is "How do I compare dates?" But I agree is not a perfect match, this is: [Calculating the Difference Between Two Java Date Instances](http://stackoverflow.com/q/1555262/1267661) and linked to it is [how to calculate difference between two dates using java](http://stackoverflow.com/q/3491679/1267661) which details some of the reasons that the accepted answer here can be wrong. – Sam Nov 27 '12 at 17:12

5 Answers5

14

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.

Bhavesh Patadiya
  • 25,555
  • 14
  • 76
  • 105
  • Thanks ,, and do u have any idea how to compare them ? in a way to check if the two dates are more than 280 day between them – Android Developer Nov 27 '12 at 08:05
  • To compare the dates you can use Date.getTime() (http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#getTime()) method. – kenota Nov 27 '12 at 08:32
  • Please read the answers to a similar question: [how to calculate difference between two dates using java](http://stackoverflow.com/q/3491679/1267661). It discusses why this approach is not reliable. (Sorry.) – Sam Nov 27 '12 at 17:15
3

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..

Ramesh R
  • 6,273
  • 2
  • 21
  • 31
Subburaj
  • 4,631
  • 6
  • 32
  • 75
1

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.

Fabian Barney
  • 13,133
  • 4
  • 37
  • 60
0

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 {
...
}
Anonymous
  • 54
  • 3
0
 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));
         }
Android Developer
  • 1,019
  • 1
  • 9
  • 30