- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to add a given time to a particular date
Following is an example to add a given time to a particular date.
Program
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar. getInstance(); cl.setTime(d1); System.out.println("today is " + d1.toString()); cl. add(Calendar.MONTH, 1); System.out.println("date after a month will be " + cl.getTime().toString() ); cl. add(Calendar.HOUR, 70); System.out.println("date after 7 hrs will be " + cl.getTime().toString() ); cl. add(Calendar.YEAR, 3); System.out.println("date after 3 years will be " + cl.getTime().toString() ); } }
Output
today is Mon Jun 22 02:47:02 IST 2009 date after a month will be Wed Jul 22 02:47:02 IST 2009 date after 7 hrs will be Wed Jul 22 09:47:02 IST 2009 date after 3 years will be Sun Jul 22 09:47:02 IST 2012
- Related Articles
- Set a GregorianCalendar object to a particular date in Java
- Java Program to Get Current Date/Time
- Java Program to parse date and time
- MySQL query to select records with a particular date and time?
- How to get a particular date in Java 8?
- How to add a date and time in HTML5?
- Java Program to format date time with Join
- Java Program to Display current Date and Time
- Java Program to add days to current date using Java Calendar
- Java Program to display date and time in Milliseconds
- Java Program to add year to current date using Calendar.add method
- How to get Time in Milliseconds for the Given date and time in Java?
- Java Program to display date and time information in lowercase
- Java Program to display date and time information in uppercase
- Java Program to convert a String to Date

Advertisements