Android: Date (year,month,day) [duplicate]
Asked Answered
P

5

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

Pert answered 27/11, 2012 at 7:53 Comment(5)
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 yearTiger
I know this way but as I said, I don't want to get every thing separatelyPert
No as I dont want to know which is before or after the otherPert
@Sam The question you posted is completely different from this question here.Petrous
@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 and linked to it is how to calculate difference between two dates using java which details some of the reasons that the accepted answer here can be wrong.Longs
T
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.

Thorndike answered 27/11, 2012 at 8:1 Comment(3)
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 themPert
To compare the dates you can use Date.getTime() (docs.oracle.com/javase/1.4.2/docs/api/java/util/…) method.Multidisciplinary
Please read the answers to a similar question: how to calculate difference between two dates using java. It discusses why this approach is not reliable. (Sorry.)Longs
C
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..

Croup answered 27/11, 2012 at 7:59 Comment(0)
P
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.

Petrous answered 27/11, 2012 at 9:57 Comment(0)
C
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 {
...
}
Couple answered 27/11, 2012 at 8:12 Comment(3)
but compareTo returns negative if secondDate is less ,, It wont work correctlyPert
You can try the date.before or afterCouple
-1 compareTo(Date) does not return the days between two dates. Furthermore your code does not even compile.Petrous
P
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));
         }
Pert answered 27/11, 2012 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.