一. 获取当前系统时间和日期并指定(格式)输出:
1. Date dat = new Date();//获取为当前系统时间
2.SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
3.df.format(dat);//将时间进行格式化得到的结果就是你要的
二.获取当前指定时间段 例如 只有(年)
1.Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
2.int year = c.get(Calendar.YEAR);
3.int month = c.get(Calendar.MONTH);
4.int date = c.get(Calendar.DATE);
5.int hour = c.get(Calendar.HOUR_OF_DAY);
6.int minute = c.get(Calendar.MINUTE);
7.int second = c.get(Calendar.SECOND);
三.获取当前时间的前几天、后几天或者后几小时
1.Calendar calendar = Calendar.getInstance();
2.calendar.add(Calendar.DAY_OF_YEAR, day);//day为前几天或是后几天(前为正值,后为负值)
3.Date time = calendar.getTime();//获取时间
4.DateFormat df = new SimpleDateFormat(DateForma);//设置显示格式
5.String nowTime= df.format(time);//得到你想要的时间