0

I've been working on a project and in my private static class MyDateEvaluator , there's a deprecated api and that is getDate().

This is my first code:

    @Override
    public boolean isInvalid(Date date) {
    return date.getDay() == 0;
    }

Then I tried to change it to Calendar.get(Calendar.DAY_OF_MONTH), but it won't work and I get an error message,

MyDateEvaluator is not abstract.

    public boolean isInvalid(Calendar Calendar) {
        return 0 == Calendar.get(Calendar.DAY_OF_MONTH);

    }
poyo
  • 23
  • 6
  • 2
    use the new `java.time` API instead of `java.util.Date` – Jens Sep 01 '19 at 13:03
  • 1
    What are you trying to do? please explain more detail – Mohammed Alawneh Sep 01 '19 at 13:03
  • 1
    Your class is supposed to have a method isInvalid(Date date) to implement its interface or extend its superclass. So you can't remove that method. You can change its implementation though (i.e. everything that is *inside* the curly braces). Please, stop using the obsolete Date and Calendar classes whenever possible. Use the java.time classes instead. They exist since Java 8. – JB Nizet Sep 01 '19 at 13:04
  • public boolean isInvalid(Date date) { return 0 == Calendar.get(Calendar.DAY_OF_MONTH); } //how do I make it to java.time?// – poyo Sep 01 '19 at 13:10
  • 1
    1. You decide which time zone to use. 2. you transform the Date to an Instant. The javadoc of Date helps. 3. You transform the Instant to a ZonedDateTime, using the chosen timezone. The javadoc of Instant helps. 3. You test if the day of the ZonedDateTime is sunday (the javadoc of ZonedDateTime helps) – JB Nizet Sep 01 '19 at 13:18
  • 1
    Note that your Calendar-based code is nonsensical: the day of month can't be 0. It goes from 1 to 31. – JB Nizet Sep 01 '19 at 13:19
  • So do I change it to .getDayOfWeek because I want to make the Sunday of each week invalid? – poyo Sep 01 '19 at 13:33
  • One of the problems is that you use the same name for the variable and the class `Calendar`. This is bad, don't ever use `Date Date`, `String String`, etc., giving variables the names of classes. Java accepts it, but you may run into strange problems. Apart from that, can you just work with `LocalDate` instead of either `Date` or `Calendar`? Are you representing a date? An instance in time? A time stamp? – RealSkeptic Sep 01 '19 at 13:52
  • Representing a day in a week, specifically sunday, and making it invalid in MyDateEvaluator. – poyo Sep 01 '19 at 13:57

2 Answers2

1

java.time

I suggest:

Set<DayOfWeek> invalidDays = EnumSet.of(DayOfWeek.SUNDAY);

@Override
public boolean isInvalid(Date date) {
    // First convert to a modern type
    ZonedDateTime zdt = date.toInstant().atZone(ZoneId.systemDefault());
    DayOfWeek dow = zdt.getDayOfWeek();
    return invalidDays.contains(dow);
}

It would be still better if you could avoid the old-fashioned Date class completely. I have assumed that the method signature is in an interface or superclass that you cannot change. If so, the parameter does need to have type Date, we cannot substitute with LocalDate (nor with Calendar as you tried).

Both Date and Calendar are poorly designed and long outdated. So the best thing we can do when we get a Date is convert it to an Instant and perform any further conversions from there. My code depends on the JVM’s default time zone, which is shaky because the setting can be changed at any time. However, given the poor design of the Date class there is no way we can avoid this.

An option that is a bit simpler than the above and may be good enough would be to declare DayOfWeek invalidDay = DayOfWeek.SUNDAY; instead of the set and compare using .equals.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try with this

 @Override
 public boolean isInvalid(Date date) {
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      return 0 == c.get(Calendar.DAY_OF_WEEK);
 }
  • 1
    This returned `false` for all dates within the next 10 days. Using the poorly designed and long outdated `Calendar` class correctly may not be so easy as it would seem at first. – Ole V.V. Sep 01 '19 at 14:16