Monday, October 21, 2013

Date Diff and Date Add

Finding the number  of years, months, days, minutes, seconds, or milliseconds between two date times can be a hard task for beginners. In this post, you will learn how to calculate the difference in days between two date times and to calculate the future and past dates from the current date. These tasks can be done simply by using the Joda-Time library. It is the most useful library to work with date and times. You will need to download this library from Joda-Time website. You will get a zip file. Please extract the zip file to a folder that you can file.

To use the Joda-Time library in your code, you need to add the jar file (joda-time-2.2.jar) to the build path of the Eclipse. You can follow the step below to add the library to the project in Eclipse.
1. From the Project menu, select Properties.
2. On the properties window, select Java Build Path. Then select Add External Jars...

Add library to Eclipse build path


3. Browse to find the jar file that you have extracted. Then click OK.

If you are using the Windows' command prompt to compile and run your java program, you will need to the add the path of the jar file to the CLASSPATH by going to the Control Panel->System->Advanced System Settings. In the System Properties dialog, select Environment Variables. In the box under User variables, double-click CLASSPATH. Then append the path of the jar file to the Variable value text box. You will use the semi-colon (;) to separate each path entry.

Now you are ready to use the Jodatime library. To calculate the difference between two date times, you can use the Years.Years.yearsBetween, Months.monthsBetween, Days.daysBetween, Hours.hoursBetween, Minutes.minutesBetween, Seconds.secondsBetween methods. The example code below calculates the days and hours between the current and another future date.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;

public class DateDiff {
public static void main(String[] args){
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyyy");

Date futureDate=null;
Date today = Calendar.getInstance().getTime();

try{
futureDate= formatter.parse("30-10-2013");
}
catch(ParseException pe)    {
   System.out.println("Parser Exception");
}

Days days = Days.daysBetween(new DateTime(today),new DateTime(futureDate));
Hours hours=Hours.hoursBetween(new DateTime(today), new DateTime(futureDate));
System.out.println(" Days Between " + today + " and " + futureDate + " :" + days.getDays());
System.out.println(" Hours Between " + today + " and " + futureDate + " :" + hours.getHours());


}
}


If you would like to calculate the future date by adding a number of years, months, days, hours, minutes, seconds, or milliseconds to the current date, you will use the plusYears, plusMonths, plusDays, plusHours, plusMinutes, plusSeconds, or plusMillis methods of the DateTime class. Similarly, to find the past date, you can use the minusYears, minusMonths, minusDays, minusHours, minusMinutes, minusSeconds, or minusMillis methods. Read the example code below.


import java.util.Calendar;
import java.util.Date;
import org.joda.time.DateTime;

public class DateAdd{
public static void main(String[] args){
Date today = Calendar.getInstance().getTime();
DateTime dt=new DateTime(today);
DateTime futureDate=dt.plusDays(2); //add two days to the current date
System.out.println("The future date is "+futureDate);


}
}


 

59 comments:

  1. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

    ReplyDelete
  2. Thanks for your informative blog!!! Your article helped me to understand the future of .net programming language. Keep on updating your with such awesome information. .net training | dot net course Chennai

    ReplyDelete
  3. In near future, big data handling and processing is going to the future of IT industry. Thus taking Hadoop Training in Chennai | Big Data Training in Chennai will prove beneficial for talented professionals.

    ReplyDelete
  4. Hi admin,
    I went through your blog, it’s totally awesome. Being most popular automation testing tool, selenium is used in automation software testing process. Selenium Course in Chennai | Best Selenium training institute in Chennai

    ReplyDelete
  5. Thanks for sharing such informative article on Loadrunner Automation testing tool. This load testing tool will provide most precise information about the quality of software. Loadrunner Course in Chennai | Loadrunner training institute in Chennai

    ReplyDelete
  6. Information about JAVA have spread all over the world wide web, but this information is quite unique and attractive. Thanks for sharing it in here. Keep blogging

    JAVA Training in Chennai
    JAVA Training Chennai

    ReplyDelete
  7. Pretty Post! Vital piece of information, I had come to know about your web- from my friend Andria, Chennai I have read nearly seven posts of yours by now, and let me tell you, your post gives the useful and the most interesting information. This is just the kind of information that I had been looking for, I’m already your RSS reader now and I feel happy about it and I love learning more about this topic.
    Regards,
    ccna course in Chennai|ccna training in Chennai|ccna training institute in Chennai

    ReplyDelete
  8. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    Python Training in Chennai|Angularjs training in chennai|Angular institutes in Chennai


    ReplyDelete
  9. Latest technology have created a greater impact over testing web applications. This vital in identifying important issues that raises in web appplications. Thanks for sharing this information in here. Keep blogging article like this.

    Selenium training in chennai | Best selenium training institutes in chennai | Big Data Training Chennai

    ReplyDelete
  10. Software testing industry offer huge career opportunity for talented professionals. software testing course in Chennai

    ReplyDelete
  11. PHP scripting is definitely one of the easiest, if not the easiest scripting language to learn and grasp for developers. This is partially due to the similarities PHP syntax has with C and Java. Even if the only knowledge of development that you have is with HTML, picking up PHP is still fairly easy.
    PHP training in Chennai|PHP training institute in Chennai|PHP course in Chennai

    ReplyDelete
  12. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. Java Training in Chennai

    ReplyDelete
  13. Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.
    Regards,
    sas training in Chennai|sas course in Chennai|sas training institute in Chennai

    ReplyDelete
  14. Dear admin, The way you have explained the concept is mezmerizing. Thank you so much for sharing tis worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.


    JAVA J2EE Training in Chennai | JAVA Training in Chennai | Android training in chennai

    ReplyDelete
  15. Thanks for sharing detailed information of unified functional testing automation tool. QTP Course in Chennai | QTP training

    ReplyDelete
  16. I agree with your post. Android software development kit makes the application development process lot simpler and effective. You can create best performing android application with ease. Android Training Institutes in Chennai | Android Training in Chennai

    ReplyDelete
  17. awesome post about learning center,which is useful and valuable information to share with others.it was an most helpful library to know about java programming codes.
    JAVA Training in Chennai

    ReplyDelete
  18. it was really good to know about these things , unique information and interesting too.

    selenium training in chennai





    ReplyDelete
  19. Really a great article.! I love to read it once again. This was an amazing post. Thanks a lot for providing this unique post for our vision

    SAS Training in Chennai

    ReplyDelete
  20. Excellent write-up as well as excellent site, I enjoy these types of demanding content hold this unique somebody.App Buzz

    ReplyDelete
  21. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The Struts, Spring, Hibernate are the advanced level of programming language which are most widely used.
    struts training in chennai | struts training

    ReplyDelete
  22. Excellent post. Thanks for sharing such a useful post.

    web design training in chennai

    ReplyDelete
  23. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Hadoop Training Chennai | PHP Training in Chennai

    ReplyDelete
  24. The usage of third party storage system for the data storage can be avoided in cloud computing and we can store, access the data through internet.
    cloud computing training in chennai | cloud computing courses in chennai

    ReplyDelete
  25. Thanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
    QTP Training in Chennai | QTP Training Institute in Chennai | QTP Training

    ReplyDelete
  26. • such a good website and given to more information thanks! and more visit...
    JAVA TESTING TRAINING IN CHENNAI

    ReplyDelete
  27. I have read your blog and I got very useful and knowledgeable information from your blog. It’s really a very nice article Spring training In Chennai

    ReplyDelete
  28. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Web Designing Course in Chennai | web designing training in chennai

    ReplyDelete
  29. Helpful post.Thanks for taking to share this.In a future everything is going to be automatic, connected and digital, embedded system engineer’s duty will be immense. An embedded system has great scope in future. Embedded Training in Chennai may get you placed in the good company.
    Regards,
    Embedded course in Chennai | AWS Training Institutes in Chennai | AWS Training center in Chennai

    ReplyDelete
  30. This data is great and amazing. A debt of gratitude is in order for taking an ideal opportunity to talk about this, I feel upbeat about it and I adore adapting more about this theme. I utilize your manual for teach my understudies.
    Thanks,
    Selenium Training institute in Chennai | Selenium Training in Chennai | Selenium Training

    ReplyDelete
  31. Java is programming language which is used in almost all the applications and games which are on the web. Java is being used extensively and it will be used extensively in near future. So getting trained in Java will surely be helpful.
    Regards,
    Best JAVA Training in Chennai | JAVA Training | JAVA Training institutes in chennai

    ReplyDelete
  32. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,

    Core JAVA Training in Chennai | JAVA Training in Chennai

    ReplyDelete
  33. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Dot net training in Chennai | dot net course in Chennai

    ReplyDelete

  34. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    Selenium Training in Chennai | Selenium Testing Course in Chennai

    ReplyDelete
  35. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    AWS Training | AWS Training in Chennai

    ReplyDelete
  36. Wonderful Post. With one of a kind substance, I truly motivate enthusiasm to peruse this post. I trust this article help huge numbers of them who looking this pretty data.
    Regards,
    Java Training in Chennai | Java courses in Chennai | J2EE Training in Chennai

    ReplyDelete
  37. Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
    Best JAVA Training institute in Chennai|Java Courses in Chennai

    ReplyDelete
  38. In near future, big data handling and processing is going to the future of IT industry. Thus taking Hadoop Training in Chennai | Big Data Training in Chennai will prove beneficial for talented professionals.

    ReplyDelete
  39. Awesome post, I would definitely say that you have explained it very well with practical scenarios. please keep sharing such articles.

    Selenium training in Chennai | Best Selenium training institute in Chennai
    Thanks a lot.

    ReplyDelete
  40. .Net is most preferred programming language among software developers all over the world. It is also considered as most trusted and effective platform to build high performing desktop or enterprise application. DOT NET Training Institutes in Chennai

    ReplyDelete
  41. Thanks for your informative article on Blogger. I think of creating my new website using blogger.
    PHP Training in Chennai

    ReplyDelete
  42. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java.Java Training in Chennai

    ReplyDelete
  43. In near future, big data handling and processing is going to the future of IT industry. Thus taking Hadoop Training in Chennai | Big Data Training in Chennai will prove beneficial for talented professionals.

    ReplyDelete
  44. I went through your blog and it’s totally awesome. Keep on updating your site with such informative post. If possible please include rss feed for your blog. Web designing Course in Chennai

    ReplyDelete
  45. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
    PHP training in chennai

    ReplyDelete
  46. Thanks for sharing informative articl. Your article helped me a lot to understand the concepts well. Salesforce Training in Chennai | Salesforce Training Institutes in Chennai

    ReplyDelete
  47. Thanks for your informative blog!!! Your article helped me to understand the future of. Keep on updating your with such awesome information.Thank you so much for sharing tis worth able content with us. The concept taken here will be useful for my future programs . Keep blogging article like this.

    softwaretesting Training in chennai

    ReplyDelete
  48. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.
    Java Training in Chennai

    ReplyDelete
  49. Thanks for your informative blog!!! Your article helped me to understand the installation process of .net programming language. Keep on updating your with such awesome information. best DOT NET Training in Chennai | .net course in Chennai

    ReplyDelete

  50. I have been reading your blog posts. You blog posts are awesome. They provide good and extremely information which is more valuable. Selenium Training in Chennai is predominant famous for Selenium Automation Training and how to get the necessary knowledge about the technology and implement it in a effective way.

    ReplyDelete