-1

So this sounds easy, just take the day and date in java and see if it's the same day using the system's time with the calendar. This I can do and this would be a question that's probably repeated multiple times on here.

But my question is how to get like Cyber Monday. Let's say I have a program and every year on Cyber Monday he needs to lower the prices in the shop by 70% or set the discount to 70%. (which is actually my goal) Should I use java calendar for it? Which I can I guess. But how should I do it? I can't just check for the day we are now is the same as 27-11-2017 because in 2018 it's not on 27-11-2018. So how should I calculate this or check for it?

Is it possible to use like a API which has annual events and where people can send a request to receive the date of the requested annual event? Like I noticed the google calendar API but this is only for calendars you made yourself. Which I'm not planning to add every annual event for my own.

I made this method, To check if it's CyberMonday, what it's main purpose is. At this moment I'm stuck doing it annually. How can I make this right for every year?

public static void CyberMonday() {
    //Check here if it's Cybermonday or if it's day after cybermonday.
    if(Server.getCalendar().getYMD().toString().equals("2017/11/27") ) {
        Config.CYBER_MONDAY = true; 
        updateCyberMondayOnWebsite();
    } else {
        Config.CYBER_MONDAY = false;
    }
}
  • 3
    SO is for specific problems you stumble upon while trying to develop. It is not for developing an idea that you have. When you have a specific code-related issue, feel free to stop back and ask. – Jason V Jul 31 '17 at 14:43
  • @Jason, Sorry you were right. This looked more like an idea, but I do have a code-related issue and that is I cant use this annually. This is why I updated my original post. – Ebert Joris Jul 31 '17 at 15:01
  • You need to **calculate** the date based on its definition. It's not a random event, it follows a pattern. – PM 77-1 Jul 31 '17 at 15:02
  • @PM77-1 Alright, I'll try that. Thanks. – Ebert Joris Jul 31 '17 at 15:03
  • 1
    I'd say determine what day thanksgiving is and add four days to that to determine cybermonday – Jon Jul 31 '17 at 16:52

1 Answers1

0

You can use Java 8's new java.time API for that.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

You can use LocalDate class to get Thanksgiving for a specified year (assuming that it's in US, so it's the fourth Thursday of November), and then get the next Monday after it. I also use the TemporalAdjusters class, which has built-in methods to get those dates. The code is:

// get Cyber Monday of a specified year
public LocalDate getCyberMonday(int year) {
    // Thanksgiving: fourth Thursday of November
    // get first day of November
    LocalDate d = LocalDate.of(year, 11, 1)
        // get the fourth Thursday
        .with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.THURSDAY));

    // next monday after Thanksgiving
    return d.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
}

With this you can get the Cyber Monday for any year:

// get Cyber Monday for year 2017
LocalDate cyberMonday = getCyberMonday(2017);

So you can compare with another dates:

// checking if today is Cyber Monday
boolean isCyberMonday = cyberMonday.equals(LocalDate.now());

If the date you want to check is a String, you must parse it first (with a DateTimeFormatter) and then compare it. I'm doing in a generic way: parsing the String, getting the year from it and comparing with the Cyber Monday of that year.

In this example, the date is in the same format of your example (2017/11/27):

String input = "2017/11/27";
// parse the input (in year/month/day format)
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate parsedDate = LocalDate.parse(input, fmt);

// compare the parsed date with the Cyber Monday of the same year
LocalDate cyberMonday = getCyberMonday(parsedDate.getYear());
boolean isCyberMonday = cyberMonday.equals(parsedDate);

You must change the pattern to match your inputs. Check the DateTimeFormatter javadoc for more information about date patterns.


PS: LocalDate.now() will get the current date in the system's default timezone. If you want to guarantee it gets the current date in a specific timezone, you can do something like LocalDate.now(ZoneId.of("America/New_York")).

Note that I used America/New_York as the timezone: the API uses IANA timezones names (always in the format Region/City, like America/Sao_Paulo or Europe/Berlin). Avoid using the 3-letter abbreviations (like CST or PST) because they are ambiguous and not standard.

You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds().