anonymous
anonymous

Reputation: 1161

Android: compare calendar dates

In my app I´m saving when I last updated some data from my server. Therefore I used:

long time = Calendar.getInstance().getTimeInMillis();

Now I want that the data is updated twice a year at 03.03 and 08.08. How can I check wheater one of these two date boarders were crossed since last update?

Upvotes: 7

Views: 26285

Answers (4)

used compareTo method ..and this returns integer value .if returns -ve the days before in current date else return +ve the days after come current date

Upvotes: 2

valerybodak
valerybodak

Reputation: 4413

If the comparison should involve only the year, month and day then you can use this method for check if c1 is before c2. Ugly, but works.

public static boolean before(Calendar c1, Calendar c2){
        int c1Year = c1.get(Calendar.YEAR);
        int c1Month = c1.get(Calendar.MONTH);
        int c1Day = c1.get(Calendar.DAY_OF_MONTH);

        int c2Year = c2.get(Calendar.YEAR);
        int c2Month = c2.get(Calendar.MONTH);
        int c2Day = c2.get(Calendar.DAY_OF_MONTH);

        if(c1Year<c2Year){
            return true;
        }else if (c1Year>c2Year){
            return false;
        }else{
            if(c1Month>c2Month){
                return false;
            }else if(c1Month<c2Month){
                return true;
            }else{
                return c1Day<c2Day;                        
            }
        }
    }

Upvotes: 4

Yoav Garreta
Yoav Garreta

Reputation: 71

There is something very important which took me a while to figure it out and can be very helpful to people out there, if you are looking for an answer to any of the following questions this is for you:

Why is my date not showing correctly? Why even when I set the time manually it is not showing right? Why is the month and the year showing one day less than the one that I set?

For some reason Java sorts the months values like an array, what I mean is that for Java January is 0 and DECEMBER is 11. Same happens for the year, if you set December as month 12 and year as 2012, and then try to do a "system.out.println" of the month and the year, it will show my month as January and the year as 2013!!

so what should you do?

Calendar cal = new GregorianCalendar();
cal.set(2012, 11, 26); // the date I want to input is 26/12/2012 (for Java, 11 is December!)

NOW WHAT IS THE CORRECT WAY TO GET THAT DATE TO SEE IT ON THE SCREEN?

if you try to "system.out.println of yourCalendar.DATE, yourCalendar.MONTH and yourCalendar.YEAR," THAT WILL NOT SHOW YOU THE RIGHT DATE!!!!

If you want to display the dates you need to do the following:

System.out.println (calact.get (calact.DATE)); 
// displays day
System.out.println (calact.get (calact.MONTH)+1); 
//add 1 remember it saves values from 0-11
System.out.println (calact.get (calact.YEAR));  
// displays year

NOW IF YOU ARE HANDLING STRINGS THAT REPRESENT DATES, OR.... IF YOU NEED TO COMPARE DATES BETWEEN RANGES , LET'S SAY YOU NEED TO KNOW IF DATE "A" WILL TAKE PLACE WITHIN THE NEXT 10 DAYS....THIS....IS.....FOR....YOU!!

In my case I was working with a string that had format "15/07/2012", I needed to know if that date would take place within the next 10 days, therefore I had to do the following:

1 get that string date and transform it into a calendar ( StringTokenizer was used here )

this is very simple

StringTokenizer tokens=new StringTokenizer(myDateAsString, "/");

do nextToken and before returning the day, parse it as integer and return it. Remember for month before returning substract 1.

I will post the code for the first you create the other two:

public int getMeD(String fecha){

    int miDia = 0;
    String tmd = "0";

    StringTokenizer tokens=new StringTokenizer(fecha, "/");
    tmd = tokens.nextToken();

    miDia = Integer.parseInt(tmd);

    return miDia;
}

2 THEN YOU CREATE THE CALENDAR

    Calendar cal = new GregorianCalendar(); // calendar
    String myDateAsString= "15/07/2012";    // my Date As String
    int MYcald = getMeD(myDateAsString); // returns integer
    int MYcalm = getMeM(myDateAsString); // returns integer
    int MYcaly = getMeY(myDateAsString); // returns integer

            cal.set(MYcaly, MYcalm, MYcald);

3 get my current date (TODAY)

    Calendar curr = new GregorianCalendar(); // current cal
    calact.setTimeInMillis(System.currentTimeMillis());

4 create temporal calendar to go into the future 10 days

Calendar caltemp = new GregorianCalendar(); // temp cal
caltemp.setTimeInMillis(System.currentTimeMillis());
caltemp.add(calact.DAY_OF_MONTH, 10); // we move into the future

5 compare among all 3 calendars

here basically you ask if the date that I was given is for sure taking place in the future AND (&&) IF the given date is also less than the future date which had 10 days more, then please show me "EVENT WILL TAKE PLACE FOR SURE WITHIN THE NEXT 10 DAYS!!" OTHERWISE SHOW ME: "EVENT WILL NOT TAKE PLACE WITHIN THE NEXT 10 DAYS".

    if((cal.getTimeInMillis() > curr.getTimeInMillis()) &&        (cal.getTimeInMillis()< curr.getTimeInMillis()))
    { System.out.println ("EVENT WILL TAKE PLACE FOR SURE WITHIN THE NEXT 10 DAYS!!");}
    else
    { System.out.println ("EVENT WILL *NOT* TAKE PLACE WITHIN THE NEXT 10 DAYS");}

ALRIGHT GUYS AND GIRLS I HOPE THAT HELPS. A BIG HUG FOR YOU ALL AND GOOD LUCK WITH YOUR PROJECTS!

PEACE. YOAV.

Upvotes: 7

Akhil
Akhil

Reputation: 14038

Change them to time in mseconds and compare:

 Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, Calendar.MARCH);
    c.set(Calendar.DAY_OF_MONTH, 3);
 long time2=   c.getTimeInMillis();
 c.set(Calendar.MONTH, Calendar.AUGUST);
 c.set(Calendar.DAY_OF_MONTH, 8);   
 long time3=   c.getTimeInMillis();
 if(time>time2){
     //Logic
     if(time>time3){
         //Logic
     }
 }

Upvotes: 23

Related Questions