I am reading about design pattern and would like to know what would be a good way to eliminate the below code duplication for the format function.
Assume I have the below code, what approach is best to take?. I can create an abstract class and inherit function or pull out the function into a separate static and make reference.
public interface Generator{
    generate()
}
public class test1 implementes Generator{
    generate()
    public static string FormatDate(){
        String date_s = " 2011-01-18 00:00:00.0"; 
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
        Date date = dt.parse(date_s); 
        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-mm-dd");
        System.out.println(dt1.format(date));
    }
}
public class test2 implementes Generator{
    generate()
    public static string FormatDate(){
    String date_s = " 2011-01-18 00:00:00.0"; 
    SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd"); 
    Date date = dt.parse(date_s); 
    SimpleDateFormat dt1 = new SimpleDateFormat("yyyymmdd");
    System.out.println(dt1.format(date));
    }
}
 
     
     
    