Results 1 to 6 of 6

Thread: Manipulation between two dates

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Manipulation between two dates

    Hello,
    I have two dates, java.util.Date, and I want to know the difference in years between two dates:
    eg between 01/03/2007 and 01/02/2009 is equal to two years ...
    Is that you know some things done that? I tried to search this on internet, but could not find one. If you have an solution for this then please do post back. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Manipulation between two dates

    Hello,
    Everything depends on what you want?
    If you think 30/12/2007 and 02/01/2009 there is 1 year then you simply do not

    Code:
    Calendar dateA = ....;
    Calendar dateB = ....;
    int dYear = Math.abs( dateA.get( Calendar.YEAR ) - DateB.get( Calendar.YEAR ) );
    I hope the above part of code will help you sort your problem and you will compile the code correctly and successfully.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Manipulation between two dates

    Hello,
    This is just an example to get an idea. Have a look at it
    Code:
    Public class TestData {
        
    
        Public TestData() {
        }
        
        Public static int diffYear(DateA Calendar, Calendar dateB )
        {   if( dateA == null | | == DateB null ) throw new IllegalArgumentException("dateA and dateB can not be null");
            
            Calendar midt = ( dateA.compareTo(dateB) > 0 ? dateB: dateA );
            Calendar mxdt = ( midt dateA ==? dateB: dateA );
            
            Calendar dudt = (Calendar)mxdt.clone();
                     dudt.set( Calendar.DAY_OF_MONTH , MinDate.get( Calendar.DAY_OF_MONTH ) );
                     dudt.set( Calendar.MONTH , MinDate.get( Calendar.MONTH ) );
                
            boolean ltyrcom = (dudt.compareTo(mxdt) < 0);
            
            int delta = mxdt.get(Calendar.YEAR) - MinDate.get(Calendar.YEAR);
            if( ! ltyrcom ) delta = delta -- 1;
         
            / / Result
            int result = Math.max( delta 0 );
            System.out.System.out.println("Difference =" + Result);
            return result;
        }
      
        / **
         * @ Param args the command line arguments
         * /
        Public static void hand(String[] args) {
            
            // 01/03/2002 - 01/02/2005 == 2
            diffYear( new GregorianCalendar(2002,02,01) , new GregorianCalendar(2005,01,01) );
            
            // 24/07/2004 - 28/09/2004 == 0
            diffYear( new GregorianCalendar(2004,06,24) , new GregorianCalendar(2004,8,28) );
            
            // 13/03/2003 - 20/07/2005 == 2
            diffYear( new GregorianCalendar(2003,02,13) , new GregorianCalendar(2005,06,20) );
            
            // 13/03/2003 - 12/03/2005 == 1
            diffYear( new GregorianCalendar(2003,02,13) , new GregorianCalendar(2005,02,12) );
        }
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    204

    Re: Manipulation between two dates

    Hello,
    Thank you for your reply and sorry to answer you so late was my turn. By cons at first I thought there was already a method to do this, am disappointed. I have tried the above methods and even the part of the code for the above post but all do not work for me. If you have any other alternative method then please do post back. Waiting for the reply. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Manipulation between two dates

    Hello,
    I think you should go for the following code
    Code:
    Calendar1 cl = Calendar.getInstance ();
    cl1.setTime (date1) / / assignment date1 in Calendar1
    int y1 = cl1.get (Calendar.YEAR);
    int d1 = cl1.get (Calendar.DAY_OF_YEAR);
    
    Calendar2 cl = Calendar.getInstance ();
    cl2.setTime (date2) / / assignment date1 in Calendar1
    int year2 = cl2.get (Calendar.YEAR);
    int day2 = cl2.get (Calendar.DAY_OF_YEAR);
    
    if (y1 == year2) (
    / * even years => difference between 2 days * /
    NbDays = day2 - d1;
    else ()
    int ecartAnnee = year2 - y1;
    NbDays = day2 + (365 * ecartAnnee) - d1;
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Manipulation between two dates

    Hello,
    Try the following code
    Code:
    Public class ElapsedTime {
    	private static Final List tmconst = Arrays.asList(new Integer[] {
    		new Integer(Calendar.Millisecond)
    		, new Integer(Calendar.SECOND)
    		, new Integer(Calendar.MINUTE)
    		, new Integer(Calendar.HOUR_OF_DAY)
    		, new Integer(Calendar.DATE)
    		, new Integer(Calendar.MONTH)
    		, new Integer(Calendar.YEAR)
    	});
     
    	Public static long elapsedTime(GregorianCalendar c1, GregorianCalendar c2, int type) {
    		return elapsedTime(c1, c2, type, false);
    	}
    	
    	Public static long elapsedTime(GregorianCalendar c1, GregorianCalendar c2, int type, boolean clear) {
    		long elapsed = 0;
    
    GregorianCalendar g1;
    GregorianCalendar g2;
    
    Integer t = new Integer(type);
    		
    		if (! tmconst.contains(t)) {
    			throw new IllegalArgumentException("Bad Calendar type required for elapsed time calculation");
    		}
    		
    		if (c1.before(c2)) {
    			GC1 = (GregorianCalendar) c1.clone();
    g2 = (GregorianCalendar) c2.clone();
    		} else {
    			GC1 = (GregorianCalendar) c2.clone();
    g2 = (GregorianCalendar) c1.clone();
    		}
    		
    		/ / At this point "g1 <= g2"
    		if (clear) {
    			/ / We only clear millisecond, SECOND, MINUTE, HOUR asked regarding the type 
    			for (int i = 0; I <= tmconst.indexOf(t) & & I < 4; I + +) {
    				GC1.set(((Integer)tmconst.get(i)).intValue(), 0);
    g2.set(((Integer)tmconst.get(i)).intValue(), 0);
    			}
    		}
    		
    		/ / Regarding what we ask for time elpased
    		/ / We use different calculation type
    		if (tmconst.indexOf(t) > 3) {
    			/ / With such a computation is responsible Calendar of leap years and more problems on date
    	        GC1.add(t.intValue(), 1);
    			while (GC1.before(g2) | | Gc1.equals(g2)) {
    		         GC1.add(t.intValue(), 1);
    elapsed + +;
    			}
    		} else {
    			/ / A faster one for milliseconds / seconds / minutes / HOUR
    			long millis = g2.getTimeInMillis() - Gc1.getTimeInMillis();
    			if (type == Calendar.Millisecond) {
    				elapsed = millis;
    			} else if (type == Calendar.SECOND) {
    				elapsed = millis / 1000;
    			} else if (type == Calendar.MINUTE) {
    				elapsed = millis / (1000 * 60);
    			} else if (type == Calendar.HOUR_OF_DAY) {
    				elapsed = millis / (1000 * 60 * 24);
    			}
    		}
    		return elapsed;
    	}

Similar Threads

  1. Content Manipulation of MS Office with XML files
    By N I C K in forum Windows Software
    Replies: 4
    Last Post: 26-03-2010, 06:39 AM
  2. Visits counter manipulation with KompoZer
    By Leiff in forum Software Development
    Replies: 4
    Last Post: 18-01-2010, 05:39 PM
  3. String manipulation in c
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 02-12-2009, 01:07 PM
  4. passwd: authentication token manipulation error
    By Knopper in forum Operating Systems
    Replies: 3
    Last Post: 29-07-2009, 11:16 PM
  5. Make lifts manipulation in Windows
    By Jackie in forum Customize Desktop
    Replies: 0
    Last Post: 10-12-2008, 01:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,632,252.95742 seconds with 16 queries