0

I am working on a small exercise to test myself. I made a small program to print the date/days in the current month. I assumed that you would be given the current date e.g. May 10th and the current day Thursday. My question, is it possible to do this exercise without being given the date today and the day? Here's my code:

public static void main(String [] args) {
        int days = 31; // may
        String arr[] = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri","Sat", "Sun"};
        int today_date = 10;
        String today_day = "Thur";
        int today_index = 0;

        //get index of current day e.g. Thur = index 3
        for(int i = 0; i<arr.length; i++) {
            if(today_day.equals(arr[i])) {
                today_index = i;
            }
        }
        //count from today_day to 1, the purpose is to get the first day of the month
        while(today_date > 1) {
            if(today_index == 0) {
                today_index = 6;
                today_date--;
            }
            else {
                today_date--;
                today_index--;
            }
        }
        //print each day of the month
        for(int i = 1; i<=days; i++ ) {
            if(today_index == 6) {
                System.out.print(i+"("+ arr[today_index]+") \n");
                today_index = 0;
            }
            else {
                System.out.print(i+"("+ arr[today_index]+") | ");
                today_index++;
            }
        }

    }

Sample output:

desc

John Cody
  • 51
  • 1
  • 1
  • 3
  • What do you mean by _My question, is it possible to do this exercise without being given the date today and the day?_ ?? – DamCx May 02 '18 at 06:23
  • For example, if not given 10(may 10th) and Thur(today). If just given "May" – John Cody May 02 '18 at 06:24
  • 2
    Why not get the current date directly using `Calendar.getInstance()` ? – DamCx May 02 '18 at 06:27
  • @DamCx Thanks for the reply, the purpose of the exercise is to not use the Calendar class. – John Cody May 02 '18 at 06:29
  • Then, if noone gives you the current date, you won't be able to do it, as you can't find it out otherwise. – DamCx May 02 '18 at 06:31
  • Just what's the point of this exercise? Not wanting to use Java's built-in libraries for handling dates is a bad idea, even if it's a test. – SeverityOne May 02 '18 at 06:40
  • 1
    You have to fix one specific date for reference, in Java, date 0 is [January 1, 1970 in UTC](https://stackoverflow.com/questions/7895171/java-date0-is-not-1-1-1970), so you can use a similar tactics. – Pham Trung May 02 '18 at 09:24

1 Answers1

0
  1. Create key value pair for mapping "integer to name of month" and "integer to day in a week". (Switch case can also be used)

  2. Use Calendar which is part of java.util package and get day of the week for given date, month and year.

Ex:

Calendar cal = new GregorianCalendar();
    cal.set(Calendar.MONTH, 4);
    cal.set(Calendar.DAY_OF_MONTH, 15);
    cal.set(Calendar.YEAR, 2018);
    System.out.println(cal.getTime());
    System.out.println(cal.getDisplayName(2, 3, Locale.US));
  1. Use "integer to day in a week" key value pair to print respective day and month in words.

Hope this helps.