Java Date Class

Java Date Class

Hi Friend, I need to have a code to get the days between specific dates. Such if I enter 19 September 2011 to 21 September 2011, the days would be saturday, sunday and monday, the code I gave simply get one day, but i need to able to enter few dates and the output should be the the days, please let me know, thanks

This is my code:

import java.util.Scanner; public class DateClassExample {

public static void main(String[] args)
{
    Scanner userIn = new Scanner(System.in);
    int dates, day, dayOfMonth, month, century, year;

    System.out.print("Enter year(e.g:2001): ");
    year = userIn.nextInt()%100;

    System.out.print("Enter month(1-12): ");
    month = userIn.nextInt();

    System.out.print("Enter days(1-31): ");
    dayOfMonth = userIn.nextInt();

    System.out.print("Days of the week are: ");

    century = year/100;

    if (month == 1){
        month = 13;
        year = year-1;
    }

    if (month == 2){
        month = 14;
        year = year-1;
    }

    day = ((dayOfMonth +((26*(month+1))/10) + year + (year/4) + (century/4) + (5*century))%7);

    if (day == 0){
        System.out.println("saturday");
    }
    if (day == 1){
        System.out.println("sunday");
    }
    if (day == 2){
        System.out.println("monday");
    }
    if (day == 3){
        System.out.println("tuesday");
    }
    if (day == 4){
        System.out.println("wednesday");
    }
    if (day == 5){
        System.out.println("thursday");
    }
    if (day == 6){
        System.out.println("friday");
    }
}

}

View Answers

Ads









Related Tutorials/Questions & Answers:
Java Date Class
Java Date Class  Hi Friend, I need to have a code to get the days between specific dates. Such if I enter 19 September 2011 to 21 September 2011... class DateClassExample { public static void main(String[] args) { Scanner
user defined date class
user defined date class  Develop a user defined date class with public.Fill the members using existing class method.implement the methods for finding current date(),days between method,advanced days and after days
Advertisements
user defined date class
user defined date class  Develop a user defined date class with public.Fill the members using existing class method.implement the methods for finding current date(),days between method,advanced days and after days
Java util date
Java util date         The class Date in "java.util" package represents... to string and string to date. Read  more at: http:/www.roseindia.net/java/java
Java Date
Java Date  Hi, What is the API for Date management in Java? How use the Java Date API for manipulating date object in Java? Provide some... of API for working with date and time objects in Java. You can also use third
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
Example of Date class
Example of Date class       We can use Date class to use current date. In this program we are creating a Date object and by using it we are going to display the current date
Java Date Conversion
Java Date Conversion In java date conversion, two packages are used... the formatting and parsing of date strings. The Calendar class of JDK 1.1 is ...; within the class returns a date/time.formatter with a default format pattern
java format date
java format date  Hi, How can I format the date in the following pattern? yyyy-MM-dd   import java.util.*; import java.text.*; class...) { Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Java String To Date
. Java provides two types of Date class from different packages...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
java class
java class  write a java program to display a msg "window closing" when the user attempts to close the window.(a) create a window class (b) create frame within the window class (c) extends window adapter class to display the msg
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 class
java class  please send me the answer of the question which i have send according of java class assoon as possible . Write a java program to display.... a. Create a window class. b. Create frame within the window class. c. Extend
What is Calendar class in Java?
This class is used for various date processing activities in Java. You can...What is Calendar class in Java?  Hi, What is Calendar class in Java? I want to learn about this class to make use of it in Java programs. Thanks
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
Get current Date and Time
; for formatting the date an time. Java provide Date class which... date, otherwise return false. Java provide one more class Calender... : Using the Date and calender class we can get the date and time in java. Using
How to get the current date in Java?
(); and with the help of Formatter class you can format the date. Check the tutorial: Java...How to get the current date in Java?  Hi, I am new to Java... in Java? Thanks   Hi, Check the tutorial: Java program to get current date
How to calculate Date Difference in Java?
How to calculate Date Difference in Java?  Hi, I have two dates variable. How to calculate Date Difference in Java? Thanks   Hi, You can use the Calendar class for calculating the date difference in Java. Get data
Date Example
class object, retrieving milliseconds from the Date object, elapsed time, date... the getTime() method of the Date class and also printing it on the console. Now... that changing its value by using the setTime() method of Date class and also printing
date and time in awt(java)
date and time in awt(java)  sir, do you have an example of date in awt java which can be view over a textfield.   Display time... class ShowTime extends JFrame{ ShowTime(){ JTextField text=new JTextField
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  i want difference between the date entered in a jtextfield and the system date in java
Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have... date to string in Java? Thanks   Hi, To convert java.util.Date object... */ /* * In this example we convert current date to String */ public class
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 Class Size
Java Class Size  How to determine size of java class
Java date add months
Java date add months In this tutorial, you will learn how to add months... to represent the current date. Then using the method add() of Calendar class, we have added 4 months to the calendar and using the Date class, we have got
Java date add year
Java date add year In this tutorial, you will learn how to add years to date... to represent the current date. Then using the method add() of Calendar class, we have added 4 years to the calendar and using the Date class, we have got the date
Date Comparison
() and after() methods of the date class. Description of program:  ... create an object comparetodaydate of the Date class that have current date... of the Date class that contains a date of 1970. Now we are comparing these two
Java class in JSP
Java class in JSP          To use a Java class method in jsp first we need to import a package inside which there is java class. We will import the package

Ads