import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class Data {
public static void main(String[] args) throws ParseException {
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。
// cal.add(Calendar.DAY_OF_MONTH, +1);//取当前日期的后一天.
System.out.println("Today is:"
+ format.format(Calendar.getInstance().getTime()));
//当前日期的第二种写法
java.util.Date today=new java.util.Date();
System.out.println("Today is "+format.format(today));
cal.add(Calendar.DAY_OF_MONTH, -1);// 取当前日期的前一天.
System.out.println("yesterday is:" + format.format(cal.getTime()));
Date d = new Date();
System.out.println("今天的日期:" + format.format(d));
System.out.println("两天前的日期:"
+ format.format(new Date(d.getTime() - (long) 2 * 24 * 60 * 60
* 1000)));
System.out.println("三天后的日期:"
+ format.format(new Date(d.getTime() + (long) 3 * 24 * 60 * 60
* 1000)));
//Date date = sdf.parse("2015-09-18");
// 比如100天后是星期几 这里打出来的是0-6的数字
int dayweek = (d.getDay() + 100 )%7;// 周天是0
System.out.println("星期"+(dayweek==0?7:dayweek));
//取日期的部分:
int year =cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH)+1;//java的月份是0-11,设置的时候记得-1 输出记得+1
int day =cal.get(Calendar.DAY_OF_MONTH);
int hour =cal.get(Calendar.HOUR_OF_DAY);
int minute =cal.get(Calendar.MINUTE);
int seconds =cal.get(Calendar.SECOND);
System.out.printf("年:%d 月:%d 日:%d %d时%d分%d秒\n",year,month,day,hour,minute,seconds);
//当前月份的最大天数
int maxDayOfthisMonth=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//取当月的第一天
java.text.SimpleDateFormat format2 = new java.text.SimpleDateFormat("yyyy-MM-01");
java.util.Date firstDay=new java.util.Date();
System.out.println(format2.format(firstDay));
//取当月最后一天
int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
java.text.Format formatter3=new java.text.SimpleDateFormat("yyyy-MM-"+maxDay);
System.out.println(formatter3.format(cal.getTime()));
//求相隔天数
Date date1=format.parse("2017-8-4");
Date date2=format.parse("2017-8-3");
long duringDay=(date1.getTime()-date2.getTime())/(24*60*60*1000);
System.out.println(Math.abs(duringDay)+1);//相邻的两天间隔视为2天
//求10小时后的时间
java.util.Calendar cal2=java.util.Calendar.getInstance();
cal2.setTime(new Date());
cal2.add(java.util.Calendar.HOUR_OF_DAY,10);
System.out.println("date:"+format.format(cal2.getTime()));//想获得具体时间看39-44行
}
}