2

I am wondering how I would go about checking to see which Saturday or Sunday it is in this year (2016)

For Example, lets say if the user enters the date 2016/01/23, It would tell the user it is the forth Saturday of the year.

Also, how can you check to make sure the user enters in a Saturday or Sunday, i.e it would ask them to enter in the input again if the user does not return a valid date that is a Saturday or Sunday in this year

Thanks for the help!

Tom
  • 21
  • 2
  • What programming language are you using? Also have you tried anything yet? – ACarter Jan 23 '16 at 20:11
  • Java, using the the SimpleDateFormat Class – Tom Jan 23 '16 at 20:17
  • Possible duplicate of [How to determine day of week by passing specific date?](http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date) – rkosegi Jan 23 '16 at 20:17
  • 1
    can you post any code you have tried? It's always best to show potential answerers what you have done so far – ACarter Jan 23 '16 at 20:22
  • 1
    @rkosegi that's pretty different - that wants day of week, this wants similar to week of year – ACarter Jan 23 '16 at 20:22
  • http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date – Abdul Razak Jan 23 '16 at 21:27
  • @AbdulRazak That possible dup was already mentioned by rkosegi. But, no, **not a dup**. This Question is about *the year*, how many Saturdays or Sundays have preceded a date for the entire year. – Basil Bourque Jan 24 '16 at 00:07

4 Answers4

4

Here are two versions demonstrating how to check if a date is a Saturday or Sunday, and how to calculate the weekday of year.

Using Calendar (all Java versions):

private static void testUtilDate(String dateString) throws ParseException {
    System.out.println(dateString);
    java.util.Date date = new SimpleDateFormat("yyyy/MM/dd").parse(dateString);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
        System.out.println("  It is a Saturday or Sunday");
    } else {
        System.out.println("  It is NOT a Saturday or Sunday");
    }

    int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
    System.out.println("  Day-of-Week in Month: " + dayOfWeekInMonth);

    int dayOfWeekInYear = ((cal.get(Calendar.DAY_OF_YEAR) - 1) / 7) + 1;
    System.out.println("  Day-of-Week in Year: " + dayOfWeekInYear);
}

Using LocalDate (Java 8+):

private static void testLocalDate(String dateString) {
    System.out.println(dateString);
    java.time.LocalDate date = java.time.LocalDate.parse(dateString);

    DayOfWeek dayOfWeek = date.getDayOfWeek();
    if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
        System.out.println("  It is a Saturday or Sunday");
    } else {
        System.out.println("  It is NOT a Saturday or Sunday");
    }

    int dayOfWeekInMonth = (date.getDayOfMonth() - 1) / 7 + 1;
    System.out.println("  Day-of-Week in Month: " + dayOfWeekInMonth);

    int dayOfWeekInYear = (date.getDayOfYear() - 1) / 7 + 1;
    System.out.println("  Day-of-Week in Year: " + dayOfWeekInYear);
}

Test

testUtilDate("2016/01/23");
testLocalDate("2016-01-23");
testUtilDate("2016/07/04");
testLocalDate("2016-07-04");

Output

2016/01/23
  It is a Saturday or Sunday
  Day-of-Week in Month: 4
  Day-of-Week in Year: 4
2016-01-23
  It is a Saturday or Sunday
  Day-of-Week in Month: 4
  Day-of-Week in Year: 4
2016/07/04
  It is NOT a Saturday or Sunday
  Day-of-Week in Month: 1
  Day-of-Week in Year: 27
2016-07-04
  It is NOT a Saturday or Sunday
  Day-of-Week in Month: 1
  Day-of-Week in Year: 27
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • good code, but he needs the ordinal of the Saturday / Sunday in the year - perhaps adjust according to my solution below? – Mr Rho Jan 23 '16 at 20:48
  • @MrRho "Day-of-Week in Year" *is* the ordinal of the Xxxday in the year, whether Xxxday is Saturday, Sunday, Monday, ..., or Friday. – Andreas Jan 23 '16 at 20:52
0

A particularly good library to use in Java is Joda Time. It has the ability to do various interval calculations so that you can determine whether something in a year is a multiple of seven - which is what you essentially want.

You will take your input date as a org.joda.time.DateTime and from there calculate the day of the year with dayOfYear(). From there it is a simple calculation to determine whether it is a Saturday or Sunday. You simply need to get the DateTime.dayOfYear of the first Saturday and Sunday and then check whether the difference in days are a multiple of seven: if((dayOfYearInput - dayOfYearFirst) % 7 == 0). If this expression evaluates to true for your dayOfYearFirst for the first Saturday or Sunday you know it's a Saturday or Sunday, otherwise it's an incompatible date.

To check which Saturday or Sunday it is, you simply have to use (dayOfYearInput - dayOfYearFirst) / 7 + 1 which will give you an ordinal of which Saturday / Sunday it is

The same approach can be used with java.util.Calendar#get(Calendar.DAY_OF_YEAR) btw - but joda time is particularly useful if you want to persist to a database for example.

If however, you want to automate the calculation of the first Saturday and Sunday for a year, you would want to iterate over the first seven dates of the year: xxxx-01-01 to xxxx-01-07 and determine whether it is a Saturday or Sunday with dayOfWeek and from there apply the previous logic. In java.util.Calendar you would use get(Calendar.DAY_OF_WEEK)

Mr Rho
  • 578
  • 3
  • 16
0

You can try this:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Temp {

    public static void main(String[] args) {
        Date date = null;
        String dateString = "23/01/2016";
        try {
            date = new SimpleDateFormat("dd/mm/yyyy").parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String[] dayOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        String day = dayOfWeek[c.get(Calendar.DAY_OF_WEEK) - 1];
        int dayOfYear = c.get(Calendar.DAY_OF_YEAR) / 7 + 1;
        System.out.println(dateString + " happens to be the #" + dayOfYear + " " + day + " of year.");
    }
}

The output of the above code:

23/01/2016 happens to be the #4 Saturday of year.

For the given date, it will print the occurrence number of that day in the year. Hope it is what you were asking.

iamharish15
  • 1,760
  • 1
  • 17
  • 20
0
public static void isValidDate(String date) throws ParseException {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new SimpleDateFormat("yyyy/M/dd").parse(date));
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

    String result = (dayOfWeek == Calendar.SATURDAY) ? "Saturday" : (dayOfWeek == Calendar.SUNDAY) ? "Sunday" : null;

    if (result == null) {
        System.out.println("Not a valid date ");
    } else {
        System.out.println("Valid date ");
        int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
        System.out.println("The Date is " + dayOfWeekInMonth + "th "+result+" of the Week in the Month");
        int dayOfWeekInYear = ((cal.get(Calendar.DAY_OF_YEAR) - 1) / 7) + 1;
        System.out.println("The date is in " + dayOfWeekInYear + "th week of the Year");

    }
}
Abdul Razak
  • 253
  • 2
  • 3
  • 14
  • Does not address the Question. How many Saturdays or Sundays have preceded in the *entire year*. I suggest fixing or deleting this Answer. – Basil Bourque Jan 24 '16 at 00:08