remi
date.java
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners

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 Pages:
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
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.