本人还没学到类和对象,想提前练习,遇到这个问题,有点迷
假设当前时间是2010年8月8日22点12分35秒,编写一个CurrentTime类,设置属性为该时间,并包括toString()方法显示该时间。。 
如果当前的时间改为2010年8月8日22点15分50秒,编写一个Demo类,改变CurrentTime类中设定时间,并打印输出。。

解决方案 »

  1.   

    Date, Calendar and SimpleDateFormat can do this job.
      

  2.   

    import java.util.Calendar;
    import java.util.concurrent.TimeUnit;public class Test01 {    public static void main(String[] args) {
            CurrentTime cur = new CurrentTime();
            System.out.println(cur.toString());
            try {
                TimeUnit.SECONDS.sleep(2L);    // 休息 2 秒钟
             } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cur.reset();
            System.out.println(cur.toString());
        }
    }class CurrentTime {
        private Calendar current;
        
        public CurrentTime() {
            reset();
        }
        
        public Calendar getCurrent() {
            return current;
        }
        public void reset() {
            this.current = Calendar.getInstance();
        }
        public String toString() {
            return String.format("%tF %<tT", this.current);
        }
    }