Singleton Pattern
Singleton Pattern Template
codesnap
21 views
    public class DateUtils {
    private static DateUtils instance;
    
    private DateUtils() {
        // 私有构造函数,防止外部实例化
    }
    
    public static DateUtils getInstance() {
        if (instance == null) {
            synchronized (DateUtils.class) {
                if (instance == null) {
                    instance = new DateUtils();
                }
            }
        }
        return instance;
    }
    
    public int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
}

  
Total PV : 22920 UV : 4849
Copyright © 2024 陕ICP备2021015553号-2