-1

I am currently trying to convert a 6 digit number into a day of the week. For example, I want "150102" to be converted as the weekday Friday "15" is the year 2015", "01" is the month, and "02" is the day

Note I want to do this without importing any time functions and using the language python.

CSCdummie
  • 3
  • 2
  • And what have you tried/got so far? Why do you not want to import any time functions (there's a lot of nuances when it comes to date/time work which is... unpleasant to work with without a dedicated API)? What (specifically) are you stuck on? – BeUndead Nov 14 '16 at 01:24
  • Are you looking for the algorithm laid out for you or just the general idea and the things to look at for. As @user2478398 mentioned, there are a lot of details when calculating time. – ninja coder Nov 14 '16 at 01:25
  • We haven't gone over any time functions in class so we can't use them. I am unable to convert a 6 digit number into a weekday – CSCdummie Nov 14 '16 at 01:26
  • I have a file with at least 100 6 digit numbers. Initially, I want to find the weekday for each. A type of def function is generally what I'm looking for – CSCdummie Nov 14 '16 at 01:29
  • Also we just want the year 2015 – CSCdummie Nov 14 '16 at 01:31

1 Answers1

1

Well, judging by your comments, this is an assignment (which means it should be marked as such). So I'm afraid my solution will be vague, but provide some guidance on a (read, probably not the best) solution.

First, you want to have something which splits the given String into the year/month/date.

public class Date {
    private int year, month, date;

    public Date(final String given) {
        // TODO - Extract year / month / date.
        // Validate year is supported, month exists and date
        // exists in month...
    }
}

Next, your gonna (probably) need some fixed point which you know the weekday of (say, Jan. 1, 2000; saturday).

Then use your knowledge of days in year/leap-year, each month, etc. to find how many days since that date the provided argument was.

static int daysSinceKnown(Date date) {
    // TODO - How many years since, how many of those were leap-years, etc.
}

Finally, modular arithmetic can give you the weekday from that function's return. (Specifically, looking at that value mod 7 (%7) would give you a number 0-6 [negative numbers won't work nicely, but you can catch that when validating the year] which you can interpret as a day of the week.)

BeUndead
  • 3,463
  • 2
  • 17
  • 21
  • So I don't think a leap year is necessary since we're just using the year 2015. Furthermore, is it possible to convert this into python? I'm struggling to read this language – CSCdummie Nov 14 '16 at 01:47
  • Fair do's on leap years. And I'm sure it is; but I'm afraid not by me (never looked at python). To be honest, there's next-to-no actual implementation code here, just placeholders to aid explaining the flow, so it shouldn't be too hard to convert out (beyond maybe making the `Date` class 3 methods, returning year/month/date). – BeUndead Nov 14 '16 at 01:52