Need to develop Java Program

Need to develop Java Program

Develop Date class in Java similar to the one available in java.util package. Use JavaDoc comments.
View Answers

August 4, 2010 at 11:45 AM

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 (!isValid(m, d, y))
throw new RuntimeException("Invalid");
month = m;
day = d;
year = y;
}

private static boolean isValid(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 (isValid(month, day + 1, year))
return new Date(month, day + 1, year);
else if (isValid(month + 1, 1, year))
return new Date(month + 1, 1, year);
else
return new Date(1, 1, year + 1);
}

public boolean isAfter(Date b) {
return compareTo(b) > 0;
}

public boolean isBefore(Date b) {
return compareTo(b) < 0;
}

public int compareTo(Date b) {
if (year != b.year)
return year - b.year;
if (month != b.month)
return month - b.month;
return day - b.day;
}

public String toString() {
return day + "-" + month + "-" + year;
}

public static void main(String[] args) {
Date today = new Date(9, 30, 2010);
System.out.println(today);
Date nextDate = today.next();
System.out.println(nextDate);
System.out.println(today.isAfter(nextDate));
System.out.println(today.next().isAfter(today));
System.out.println(today.isBefore(nextDate));
System.out.println(nextDate.compareTo(today));
}
}

Thanks









Related Tutorials/Questions & Answers:
Need to develop Java Program - Development process
Need to develop Java Program  Develop multi-threaded echo server and a corresponding GUI client in Java
Need to develop Java Program - Development process
Need to develop Java Program  [Mini-Project] Develop a programmer's editor in Java that supports syntax-highlighting, compilation support, debugging support, etc
Advertisements
Need to develop Java Program - Development process
Need to develop Java Program  Develop a simple OPAC system for library using even-driven and concurrent programming paradigms of Java. Use JDBC to connect to a back-end database
Need to develop Java Program - Development process
Need to develop Java Program  Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read
Need to develop Java Program - Development process
Need to develop Java Program  Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers
Need to develop Java Program - Development process
Need to develop Java Program  Develop Date class in Java similar to the one available in java.util package. Use JavaDoc comments.  Hi Friend, Try the following code: public class Date { private final int
need a java program for the following
need a java program for the following  Write a java program with three horizontal sliders. Name the sliders, the first one as red, the second one as green and the third one as blue. You design the program such that on varying
need program - Java Beginners
need program  I need a java program to get the multiple student name and mark.display the student information by sorting the marks in ascending or desending order.  Hi Friend, Try the following code: import
Need a java program
Need a java program  Write a code of java to print the fibonacci series.   for(int j=1;j<=5;j++) { int i,a,b,c; a=1;b=1;int n=5; System.out.print("fibonicci series is " + a +" " + b
need help with a program - Java Beginners
Java algorithm - need help with a program  Java algorithm - need help with a program
need help with a program - Java Beginners
need help with a program   Part I An algorithm describes how... by the user. The output of the program should be the length and width (entered.... First you would need to make up the "test data". For this algorithm, the test data
need a java program - Java Beginners
need a java program  Draw a bar chart for the following data using frames Year: 2000 2001 2002 2003 2004 2005 TurnOver:7 8 4 5 9 12
I need to develop a gui like this
I need to develop a gui like this  Hai Friends, I need to develop such a menu in my gui programs. The gui contains 1 text field.whenever the text... please help me   sorry goes through this link I need to develop like
need to Program
need to Program   Can any one help me on below How to insert more then one records in EXCEL Sheet using JSP
Desperately need Java program ASAP - Java Beginners
Desperately need Java program ASAP  I need the source code for a java application that asks the user to enter a 10 character telephone number... should display 555-438-3663. I need this program ASAP if possible. Thank you
I need add my java program with a picture.
I need add my java program with a picture.  Good evng Frnds Friends i created 1 jar file in my desktop. I need add this program with 1 picture. Whenever the user double clicking on that picture The program must start instead
need help on writing a program. - Java Beginners
Writing first Java Program  Hi, I need help to write my first Java Program other then Hello World! Thanks!!  Hi Thanks for choosing roseIndia to get your query solved. check given link to see the solution of your Java
need help with java program as soon as possible
need help with java program as soon as possible  So my assignment is to write a program that used while loops to perform 6 different things. 1. prompt the users to input two integers: firstNum and secondNum (firstNum must be less
need help creating a lift program - Java Beginners
need help creating a lift program  Classes, Methods, Constructors please i need help to create an elevator program Simulating an Elevator write an Elevator class containing various methods. Then, write a program
i need program or code for this program
i need program or code for this program  out should be in this form 1 2 3 4 5 6 3 5 7 9 11 8 12 16 20 20 28 36 48 64 112
please give me an idea to develop a program for this question?
please give me an idea to develop a program for this question?  How to enter a text or number from keyboard without using InputStreamReader method in java
need help with program
need help with program  To write a program to read a set of words from a file and return the following 1)Each word in the file against its frequency 2) the frequency should be in the descending order of the frequencies
need help with program
need help with program  To write a program where we can take two integer array as input,Find the missing number from array"B" has all the numbers from array"A" except one,and find the fastest way in doing
I really need a tutor for Java program that has to do with multithreading and gui!
I really need a tutor for Java program that has to do with multithreading and gui!  I am looking for a Tutor to help me with a Java program, specially GUI and multithreading. If you can tutor, please email me
Need help with console program?
Need help with console program?  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). thanks in advance.   Here is an example that repeatedly
java program...need help as soon as possible!!! - Java Beginners
java program...need help as soon as possible!!!  Modify the Lexer (15.... 4. Print each token with line number READLINE: program { int i int j program left: 0 right: 6 line: 1 { left: 8 right: 8: line: 1 int
Need to develop a control panel for application form which is developed in jsp
Need to develop a control panel for application form which is developed...,radio buttons and text boxes in jsp this for user to fill it. Now I need to develop a control panel for that application page at the admin side in order
develop code - Java Beginners
develop code   i want to develop a program that search a file in a directory and its subdirectories and rename that file with name of directory
Need help writing a console program
Need help writing a console program  I need help cant seems to figure it out! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a string to lower
need of java coding - JavaMail
need of java coding  Design a java interface for ADT stack. Develop it and implement the interface using link list. Provide necessary exception handling for that implementations. pls mail me this lab program..its urgent
How to Develop, Compile & Run a rmi Program in NetBeans 6.9.1 ide?
How to Develop, Compile & Run a rmi Program in NetBeans 6.9.1 ide?  how to develop, compile & run a rmi program in netbeans 6.9.1 ide? will you please give me a step by step (practical) explaination? Thanks in advance
really need help on how to write this program plz some 1 help out. - Java Beginners
really need help on how to write this program plz some 1 help out.  i am confused here on what to write can some 1 help out here i dont quite... a Java program that displays the following prompts: Enter
beginner need help with online ordering program please
beginner need help with online ordering program please  I have created a program for an online ordering system and now have to add error handling and a few other things my teacher doesn't teach well is there a template I can go
Java concurrency
makes a program short, fast, easier to write, read and maintain. Java... easy to programs as you don't need to develop a standard class, just use...Java concurrency utilities are classes used as building blocks in building
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
Need the program urgent veryyyyyyyyyyyyy urgent plsssssssssssssssssss follow the standard in the program
Need the program urgent very urgent please follow the standard in the program  Restaurant Order Management System (ROMS) Problem Case: Create a ROMS system, which is intended to take an order from the customer and charge
First Java Program
First Java Program       In this section you will learn how to develop your first Java program... for the Java Virtual Machine.  You can write a Hello World program as your
need sourcecode - Java Beginners
need sourcecode  Hai, I need program for packetization for videos
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
i need program to insert data in database using javascript
i need program to insert data in database using javascript  please help me
Need help in completing a complex program; Thanks a lot for your help
Need help in completing a complex program; Thanks a lot for your help  Hi, I am currently working on one program which works in this way. 1... content is user inputted and static content is prewritten. 3) We need to search
java program
java program  . Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time
Need Help in Java programming
Need Help in Java programming  Hello. Can someone please help me with writing the following program Java program that gives assignment details such as:assignment number,assignment name,due date,submission date,percentage marks

Ads