Share on Google+Share on Google+

remi
date.java
1 Answer(s)      7 years ago
Posted in : Java Beginners

Ads

View Answers

July 22, 2009 at 4:48 PM


Hi Friend,

Try the following code:

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateExample {
public static void main(String[] args) {
try {
DisplayDate d=new DisplayDate(3,30,2009);
for(int i=0; i<365; i++){
d.incrementDay();
System.out.println(d);
}
} catch (Exception e) {}
}
static class DisplayDate {
private Calendar cal;
public DisplayDate(int month, int day, int year) throws Exception {
cal=new GregorianCalendar();
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.YEAR, year);

if(cal.get(Calendar.YEAR)!=year || cal.get(Calendar.MONTH)+1!=month || cal.get(Calendar.DATE)!=day){
System.out.println("Invalid Date");
}
}
public Date getDate() {
return cal.getTime();
}
public void incrementDay() {
cal.add(Calendar.DATE, 1);
}
public String toString() {
return String.format("%d/%d/%d", cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE), cal.get(Calendar.YEAR));
}
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
date.java - Java Beginners
date.java  Hi, this my first time taking java so could you help me with this problem, thank you : Create a program called Date.java to perform error-checking on the initial values for instance: fields month, day and year. Also
Date Java - Java Beginners
the 'public' modifier from the Date.java class. Thanks
Advertisements
Two compilation errors.Can anyone help soon. - Java Beginners
Two compilation errors.Can anyone help soon.  a program called Date.java to perform error-checking on the initial values for instance fields month, day and year. Also, provide a method nextDay() to increment the day by one
Java Increment Date. - Java Beginners
Java Increment Date.  Create a program called Date.java to perform error-checking on the initial values for instance: fields month, day and year. Also, provide a method nextDay() to increment the day by one. The Date object
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads