Beware: If you are replacing a sensitive piece of data with a value that can be repeatedly determined, you have not really scrubbed your data. If your purpose is to protect sensitive data, such as HIPAA, I suggest your consult someone who is in charge. They should be trained on how to appropriately scrub data.
Another point to clarify: Your title is a contraction. A random value cannot be predictably repeated, by definition.
java.time
Your code example is using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310. For a date-only value, use LocalDate
class.
Just assign an arbitrary number of days
If you want an arbitrary yet repeatable adjustment, just add or subtract a certain number of days. You could arbitrarily assign a negative number (subtraction) for a date whose day number is odd, and assign a positive number (addition) for a date whose day number is even.
To determine even or odd number, see this Question.
int daysToAddToOddDayNumber = -2_555 ;
int daysToAddToEvenDayNumber = 2_101 ;
LocalDate localDate = LocalDate.of( 1970 , Month.JANUARY , 1 );
boolean isEven = ( ( localDate.getDayOfMonth() & 1) == 0 ) ;
LocalDate adjusted = isEven ? localDate.plusDays( daysToAddToEvenDayNumber ) : localDate.plusDays( daysToAddToOddDayNumber ) ;
Dump to console.
System.out.println( "localDate.toString(): " + localDate ) ;
System.out.println( "adjusted.toString(): " + adjusted ) ;
See this code run live at IdeOne.com.
localDate.toString(): 1970-01-01
adjusted.toString(): 1963-01-03
Obscure the number of days to be added
You could get fancy a bit by taking a hash of the value of the date, then use that hash result to determine a number of days to be added. Again, as I said before, this may not qualify as sufficient scrubbing depending on the needs (and laws!) of your project.
LocalDate localDate = LocalDate.of( 1970 , Month.JANUARY , 1 );
String input = localDate.toString();
MessageDigest md = null;
try
{
md = MessageDigest.getInstance( "MD5" );
md.update( input.getBytes() );
byte[] digest = md.digest();
int days = new BigInteger( 1 , digest ).mod( new BigInteger( "10000" ) ).intValue();
LocalDate adjusted = localDate.minusDays( days );
System.out.println( "localDate = " + localDate );
System.out.println( "input = " + input );
System.out.println( "days = " + days );
System.out.println( "adjusted = " + adjusted );
} catch ( NoSuchAlgorithmException e )
{
e.printStackTrace();
}
See this code run live at IdeOne.com.
localDate = 1970-01-01
input = 1970-01-01
days = 8491
adjusted2 = 1946-10-03