This is my code:

public boolean SetYear (String year)
		if (year.matches("[0-9]{4}"))
		return true;
    return false;

Instead of return false I want to make a runtime error with my massage. Something like "the value must be 4 digits"
How can I do it?

Or if you want to generate any old runtime error, I've learned plenty of ways to do that in my time programming. :)

One of the most common is indexing an array by an index too low or too high

Define your own exception handler and use try/catch

Most probably you mean define his own (custom) exception class, but this would be just an overkill.
Ezzaral's suggestion of throwing an IllegalArgumentException for me hits the nail on the head.

Yes, generally you only need to create a custom exception if you are wanting to pass along additional information that wouldn't be possible with one of the standard exceptions.

This article has been dead for over six months. Start a new discussion instead.