Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help determining which day a particular date falls on.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help determining which day a particular date falls on.

    So for this project, we have to calculate which day of the week a particular date falls. For example, given 11/3/2012, you would need to find out on which day March 11th falls. (It goes dd/mm/year).

    the ArrayList named Rob contains all the tokenized values of the inputs, which are imported from a text file. (An example input would be 12/4/1992,19/3/1996)

    I have calculated whether or not a certain year is a leap year, and I have calculated on which day of the week January 1st falls. I am stuck at the part where I determine the which day of the week a particular date falls when given the information that I have already found.

    How would I determine which day of the week a date (such as 20/7/1966) falls?

    What do I need to add to the code to make this happen?



    PS - Everything up to this point works properly.


    import java.util.*;
    import java.io.*;
     
    public class DayOfTheWeek {
     
     
    private static PrintWriter out;
    private static Scanner in;
    private static int integer;
    private int year;
    private static int intDay;
    private static int intMonth;
    private static int [] temp;
    private static int numDays;
     
     
    public static void main (String [] args) { 
     
     
    int [] monthlength = new int [] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    boolean leapYear;
     
     
    try{
     
    FileInputStream fstream = new FileInputStream("input.txt");
    // Get the object of DataInputStream
     
     
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
     
     
    //Read File Line By Line
    while ((strLine = br.readLine()) != null) { 
     
    //split it using /and , as the delimiter
     
    StringTokenizer st = new StringTokenizer(strLine);
    st = new StringTokenizer(strLine, "/,");
    while (st.hasMoreElements()) {
     
     
    String token = st.nextToken();
    integer = Integer.parseInt(token);
    rob.add(integer);
    }
    }
     
     
     
     
     
     
    for (int i = 2; i < rob.size(); i+=3) {
     
     
    int temp = rob.get(i);
    int y = (temp-1);
    int x = (36 + y + (y/4) - (y/100) + (y/400)) % 7;
    int daysinYear = 365;
     
     
     
    if (rob.get(i) % 400 == 0) {
    leapYear = true;
    daysinYear = (daysinYear + 1);
    }
     
    else if ((rob.get(i) %4 == 0) && (rob.get(i) %100 != 0)) {
    leapYear = true;
    daysinYear = (daysinYear + 1);
    }
     
    else {
    leapYear = false;
     
    }
     
    if (x == 0) {
    System.out.println("January first is a Sunday.");
    System.out.println(temp);
    }
    if (x == 1) {
    System.out.println("January first is a Monday.");
    System.out.println(temp);
    }
    if (x == 2) {
    System.out.println("January first is a Tuesday.");
    System.out.println(temp);
    }
    if (x == 3) {
    System.out.println("January first is a Wendsay.");
    System.out.println(temp);
    }
    if (x == 4) {
    System.out.println("January first is a Thursday.");
    System.out.println(temp);
    }
    if (x == 5) {
    System.out.println("January first is a Friday.");
    System.out.println(temp);
    }
    if (x == 6) {
    System.out.println("January first is a Saturday.");
    System.out.println(temp);
    }
     
     
     
    }
     
     
    FileWriter output = new FileWriter(args[0]);
    PrintWriter out = new PrintWriter(output);
     
     
    // Write text to file
    out.println("This is line 1");
    out.println("This is line 2");
    out.println("this is line 3 part 2");
    out.close();
     
     
    //Close the input stream
    in.close();
    }
     
    catch (Exception e){
    System.err.println("Error: " + e.getMessage());
    }
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Help determining which day a particular date falls on.

    Why don't you just use the Calendar class?

    If you can't, then think about it this way: how would you do this by hand, without a computer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Help determining which day a particular date falls on.

    The easiest way would be to first assign a number to each day of the week.(1-7) Then find how many days are in between the current day and the future
    date. Then mod by 7. That will give you the day of the week.

Similar Threads

  1. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  2. Basic OOP having trouble determining how to handle methods
    By Nikkon2131 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2011, 06:15 PM
  3. Date help
    By banny7 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 18th, 2011, 10:41 AM
  4. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM
  5. Determining Linear Approximations
    By ddunlop07 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 11:12 PM