Since you're using JDK 21 as your base, you can also run java using the --source flag, e.g.
javac --source 17 MyClass.java
That way it will use Java 17 language features only, even though you're running JDK 21. I'm not sure whether it also limits the standard library features you can use. It didn't use to, but I think it may now.
If you're using an
IDE, it should also be possible to configure the IDE to do the same thing for you.