How to get the Number of days

Discussions

Web tier: servlets, JSP, Web frameworks: How to get the Number of days

  1. How to get the Number of days (11 messages)

    Hai I have 2 dates in the form mm/dd/yyyy.
    I want to get the number of days between those dates.
    Helps appreciated,its urgent.
    Thanx

    Threaded Messages (11)

  2. How to get the Number of days[ Go to top ]

    It can't be done. Not with today's technology anyway.
  3. How to get the Number of days[ Go to top ]

    It cant be done??? Unless Im not understanding the question, here is an easy way.

    public int getDaysBetween(String start,String end){
    Calendar startDate = Calendar.getInstance();
    startDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(start));
    Calendar endDate = Calendar.getInstance();
    endDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(end));
    return endDate.get(Calendar.DAY_OF_MONTH) - startDate.get(Calendar.DAY_OF_MONTH);

    }
    This will return the days between your start and end dates.

    Hope this helps.
  4. How to get the Number of days[ Go to top ]

    Yes it can be done. I don't have the code here at work, but there is a dating system (Cartitian, or something like that) that converts a date from a CCYYMMDD format to a straight forward number, then all you do is minus one number from the other and the result is the number of days between the two. This dating system takes into account the change of month, change of year and even leap years. I don't net access from home at the moment, so I can't supply the code, but you should be able to find it on the net somewhere.... Sorry I can't be more specific than that.

    HTH

    Eric
  5. How to get the Number of days[ Go to top ]

    Thanx a lot eric
    I appreciate and please send me that code whenever u feel free.Now Iam doin in the backend itself and its working fine.
  6. How to get the Number of days[ Go to top ]

    did you try that Pabak Nanda's code?

    >public int getDaysBetween(String start,String end) throws >ParseException
    >{
    >Calendar startDate = Calendar.getInstance();
    >startDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse >(start));
    >Calendar endDate = Calendar.getInstance();
    >endDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse >(end));
    >
    >Date startD = startDate.getTime(); // get the Date Object.
    >Date endD = endDate.getTime();
    >// getTime of Date returns time in Milisecond.
    >return (int)((endD.getTime() - startD.getTime())/ >(1000*60*60*24));
    >
    >}


    It seems that it could work.


    >Please send me all of your code too. I'm tired of writing >my own.

    go tell that to your boss.. "i'm tired of writing my own code, desided to wait till someone emails it to me" =)
  7. How to get the Number of days[ Go to top ]

    Eric,

    Please send me all of your code too. I'm tired of writing my own.
  8. How to get the Number of days[ Go to top ]

    hehehehe, you guys are funny :)

    Um, as I said, I DON'T have net access at home, at the moment. And it'll probably be about another month or so before I do get net access. So, until then, sorry I can't help. But, I will share with you when the time comes....

    Cya,
    Eric
  9. How to get the Number of days[ Go to top ]

    This code will not work because it only compares dates within the same month of the same year.

    Jeff's sarcasm aside, it is possible with today's technology to get the number of days between two dates. Jeff, Java technology has advanced much in the last few years to the point where you can easily subtract numbers :-)

    There's an article in June's Java Developer's Journal that talks about time and date in Java, you should look at that. Otherwise, open on the Java API and this method is probably already implemented since *everything* in Java is already done for you. If not, you can always think about it a little and come up with your own implementation. Off the top of my head you can compare absolute time differences or compare years, then months, then day and combine those differences to get your total number of days.

    --
    Tinou
    www.tinou.com
  10. How to get the Number of days[ Go to top ]

    Yes One way of doing what I thought is after getting the date in dd/mm/yyyy format we can use the StringTokenizer to separate the month and day and year and then find out the difference.But the problem with this is again I have to find out for any Feb is there or the number of days in that particular month,its again goin to crack our mind right.
  11. How to get the Number of days[ Go to top ]

    This is just an modification to Weston Aiken's Code. As java provides the way to calculate the number of mili second, we can get the number of days by just simple calculation.

    Hope this code will help.

    public int getDaysBetween(String start,String end) throws ParseException
    {
    Calendar startDate = Calendar.getInstance();
    startDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(start));
    Calendar endDate = Calendar.getInstance();
    endDate.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(end));

    Date startD = startDate.getTime(); // get the Date Object.
    Date endD = endDate.getTime();
    // getTime of Date returns time in Milisecond.
    return (int)((endD.getTime() - startD.getTime())/(1000*60*60*24));

    }
  12. How to get the Number of days[ Go to top ]

    Thanx a lot for ur reply,I appreciate.