课堂练习2:
打印当前时间。学习使用Date类和Calendar类。(教材P194页)
import java.util.*;
public class PrintTime {
public static void main(String args[]) {
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
int year=calendar.get(Calendar.YEAR),
month=calendar.get(Calendar.MONTH)+1,
day=calendar.get(Calendar.DAY_OF_MONTH),
hour=calendar.get(Calendar.HOUR_OF_DAY),
minute=calendar.get(Calendar.MINUTE),
second=calendar.get(Calendar.SECOND);
System.out.print("现在的时间是:");
System.out.print(""+year+"年"+month+"月"+day+"日");
System.out.println(""+hour+"时"+minute+"分"+second+"秒");
}
}
运行结果:
感悟:学得好快。。。没有深入理解。。大概就是创建对象,对象加点调用方法,然后输出打印