Welcome to collectivesolver - Programming & Software Q&A with Examples. A website you can trust. All programs tested and work.

Contact: aviboots(AT)netvision.net.il


Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Survey Junkie is the #1 place online for your audience to earn rewards for their opinion.

100%
Managed VPS Hosting
Affordable. Secure. Your Own Virtual Private Hosting. Faster than AWS and Rackspace

Bonsai Boy of New York Bonsai Trees and Accessories
Liquid Web - Highest Level of Performance and Security with Managed Dedicated Servers

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

21,340 questions

28,142 answers

573 users

How to add two dates in Java

1 Answer

0 votes
import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {  
        Calendar d1 = Calendar.getInstance();
        Calendar d2 = Calendar.getInstance();
        
        System.out.println(d1.getTime());
        System.out.println(d2.getTime());

        Calendar d1plusd2 = (Calendar) d1.clone();
        
        d1plusd2.add(Calendar.YEAR, d2.get(Calendar.YEAR));
        d1plusd2.add(Calendar.MONTH, d2.get(Calendar.MONTH) + 1); 
        d1plusd2.add(Calendar.DATE, d2.get(Calendar.DATE));
        d1plusd2.add(Calendar.HOUR_OF_DAY, d2.get(Calendar.HOUR_OF_DAY));
        d1plusd2.add(Calendar.MINUTE, d2.get(Calendar.MINUTE));
        d1plusd2.add(Calendar.SECOND, d2.get(Calendar.SECOND));
        d1plusd2.add(Calendar.MILLISECOND, d2.get(Calendar.MILLISECOND));
        
        System.out.println(d1plusd2.getTime()); 

    }
}
    
   
   
    
/*
run:
   
Sat Jan 15 10:22:31 GMT 2022
Sat Jan 15 10:22:31 GMT 2022
Tue Mar 01 20:45:02 GMT 4044

*/

 




answered 6 days ago by avibootz
...