Question: Java Increment Date.

Ads

 
 
 

Share on Google+Share on Google+

Anna
Java Increment Date.
1 Answer(s)      6 years and 10 months ago
Posted in : Java Beginners
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 should always remain in a consistent state. Write a program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. Test the following cases:

a) incrementing into the next month.

b) incrementing into the next year

What I have so far:
public class Date {

private int day; // 1-31
private int month; // 1-12
private int year; // The year!

// Get Methods
// get the Day
public int getDay()
{
return day;
}

// get the Month
public int getMonth()
{
return month;

}

// get the Year
public int getYear()
{
return year;

}

// Set Methods.
public void setTime( int d, int m, int y )
{
setDay( d ); // set the day
setMonth( m ); // set the month
setYear( y ); // set the year
}

private void setDay(int d) {
}
public Date() { setDay( 07, 20, 2010 ); }

// Time3 constructor: day, month and year supplied.
public Date( int d, int m, int y ) { setDay( d, m, y ); }

// set the day
public void setDay( int d, int i, int j )
{ day = ( ( d >= 0 && d < 32 ) ? d : 0 ); }

// set the month
public void setMonth( int m )
{ month = ( ( m >= 0 && m < 12 ) ? m : 0 ); }

// set the year
public void setYear( int y )
{ year = ( ( y >= 0 && y > 0 ) ? y : 0 ); }

// Change day to next day!
public void nextDay()
{
setDay( day + 1 );
if ( day == 0, day<=31 )
nextDay();
}

// Increment the Month
public void incrementMonth()
{
setMonth( month + 1 );

if ( month == 0, month <12 )
incrementMonth();
}

// Increment the Year
public void incrementYear()
{
setYear( year + 1 );
}
// Convert to String in standard-date format
public String toString()
{
return ( ( month == 12 || month == 0 ) ? 12 : month % 12 ) +
"/" + ( day < 31 ) + year +
"/" + ( year > 0 );
}
}
Ads
View Answers

July 23, 2010 at 12:01 PM


Hi Friend,

Try the following code:

public class Date {

private final int month;
private final int day;
private final int year;

public Date(int m, int d, int y) {
if (!increment(m, d, y))
throw new RuntimeException("Invalid");
month = m;
day = d;
year = y;
}
private static boolean increment(int m, int d, int y) {
int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (m < 1 || m > 12)
return false;
if (d < 1 || d > DAYS[m])
return false;
if (m == 2 && d == 29 && !isLeapYear(y))
return false;
return true;
}
private static boolean isLeapYear(int y) {
if (y % 400 == 0)
return true;
if (y % 100 == 0)
return false;
return (y % 4 == 0);
}
public Date next() {
if (increment(month, day + 1, year))
return new Date(month, day + 1, year);
else if (increment(month + 1, 1, year))
return new Date(month + 1, 1, year);
else
return new Date(1, 1, year + 1);
}
public String toString() {
return day + "-" + month + "-" + year;
}
public static void main(String[] args) {
Date today = new Date(9, 30, 2010);
System.out.println(today);
for (int i = 0; i < 35; i++) {
today = today.next();
System.out.println(today);
}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Java Increment Date. - Java Beginners
Java Increment Date.  Create a program called Date.java to perform.... Also, provide a method nextDay() to increment the day by one. The Date object... (y % 4 == 0); } public Date next() { if (increment(month, day + 1, year
Stumped with NOT NULL AUTO_INCREMENT
to create a new table with the ID column being NOT NULL AUTO_INCREMENT. The table will create if I do not have NOT NULL AUTO_INCREMENT, but it WILL NOT create..._INCREMENT, user_id INT(11), courseid INT(10
Advertisements
Java Date conversion - Date Calendar
Java Date conversion  How do I convert 'Mon Mar 28 00:00:00 IST 1983' which is a java.util.Date or String to yyyy/MM/dd date format in sql
Date Java - Java Beginners
main( String args[] ){ System.out.println( "Checking increment" ); Date...Date Java  I'm using the eclipse software and I get an error for my...: public class Date { private int month; // 1-12 private int day; // 1-31
Mysql Date To Java Date
Mysql Date To Java Date       Mysql Date To Java Date retrieve the records from Mysql and display the date in java. Understand with Example The Tutorial illustrate an example from
Conditional increment in xslt
Conditional increment in xslt  In a Shipment the number of orders..., there is no need to increment hierarchy. points: There is only one shipment Number... levels(increment value),but when comes to orderLineItems one condition
Conditional increment in xslt
Conditional increment in xslt  In a Shipment the number of orders..., there is no need to increment hierarchy. points: There is only one shipment Number... levels(increment value),but when comes to orderLineItems one condition
Conditional increment in xslt
Conditional increment in xslt  In a Shipment the number of orders..., there is no need to increment hierarchy. points: There is only one shipment Number... levels(increment value),but when comes to orderLineItems one condition
How to compare date in Java?
How to compare date in Java?  Hi, How to compare date in Java? Thanks   Hi, Check this example Date Comparison. Thanks
how to add date and change date in java - netbeans
how to add date and change date in java - netbeans  I need to code use to increase date How to get date after 30 days using netbeans
java Date - Java Beginners
java Date  Dear sir I have an assignment, It is .. "Develop Date class in java similar to the one available in java.util package.Use JavaDoc..., Try the following code: public class Date { private final int month
java - Date Calendar
Java convert string to date  I need an example in Java to convert the string to date.  Convert string to date formatimport... str_date="04-04-2008"; DateFormat formatter ; Date date
date picker - Java Beginners
date picker  sir i need code for date picker in java swing because for Embedding in my project plz sir
Display Date - Java Beginners
Display Date  Plz send me sample java program that display day, date... to display day,date,year and current time. import java.util.*; class DateExample { public static void main(String[] args) { Date date = new Date
PHP Increment Decrement Operator
of increment and decrement, much like C,C++. but in few cases it follows Perl style...; whereas in 'C' and 'Java' result would be '['. Examples: <?php $a=12; echo "Pre increment value of a is:"
Java Date - Java Beginners
Java Date  I have to find the previous date's beginning time and end time in the long milliseconds format. for example today date is '23rd April... main(String[] args) { Date now = new Date(); DateFormat df   Hi friend
Java programme - Date Calendar
Java programme  Can you provide me the coding for Date class provided by java util package
Parsing date in Java
Parsing date in Java  How can i parse the date in Java in a simple format..?   SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy
java format date
java format date  Hi, How can I format the date in the following...) { Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String formattedDate=sdf.format(date
date - Java Beginners
date  1.Write a program to check input date is valid or not using java(value must be given by user)  Hi Friend, Try the following code... DateValidation{ public boolean isValidDate(String date){ SimpleDateFormat sdf = new
MySQL Auto_Increment
MySQL Auto_Increment This example illustrates how to define AUTO_INCREMENT of a field. The AUTO_INCREMENT attribute can be used to generate a unique... AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id
turbo c pre-increment question
turbo c pre-increment question  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times
Java Date
The Java Date class allows the developers to manipulate date and time from within the Java program. The Java Date class is very useful class and it is used... in your program you will have to use the Java Class to manipulate the date
java yesterday - Date Calendar
java yesterday  And how to make date format DD/MM/YYYY,thank's ... static void main(String args[]){ Date todaysDate = new java.util.Date... formattedDate = formatter.format(todaysDate); System.out.println("Formatted date
java date - Java Beginners
java date   Hi, Please observe the below code: import... is "login" the name,date,time is automaticaaly inserted into the database . My doubt is iam storeing system date and system time but i want to store i server
ask java - Date Calendar
but in java i found too but i can't to join in another frame. And i want to ask how to subtract a date, ex:i work since 2008/09/01 so i'am work until now 1 month 10... { public static void main(String[] args) { Date date = new Date
Date picker in Java Swing
Date picker in Java Swing  Hello Sir, Sir ,I am in need of Time Picker just like the Date picker available in java Swing. Kindly... link: Java Date Time Picker Example   Hello Sir, Thank
java yesterday - Date Calendar
java yesterday  Afternoon,Yesterday i have a question that how to make date sustract, the result is to make 1 month and 1 day like this, right. now how about 1 year and 1 month? And Can help me to using Date Calender what
java - Date Calendar
java   Using java how can i change the date format of the system from mm/dd/yy to dd/mm/yy. The code which i wrote is changing the format only on the console and not on the system's control panel.Can you suggest me anything
date problem - Java Beginners
date problem  how to extract day,month,year hours ,second from a string date. Examples: String dat ="200912202934" from this string extract month ,year,date,hours,minute Please help me. Thanks in advance Sushil 
Java String To Date
Java String To Date In this section you will read about how to convert... be convert into java.util.Date. Java string to date convert explains the conversion of Java string written into the specified format to a Java date object
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads