Date
Date date = new Date();
获取时间
Date d = new Date()
// Date d2=new Date(System.currentTimeMillis()-24*60*60*1000)
System.out.println(d)
// System.out.println(System.currentTimeMillis())
Calender
- SimpleDateFormat:规范日期格式
设置当前基准时间
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
// add方法:增加或减少时间值
cal.add(Calendar.DATE, 2);
//System.out.println(cal.getTime());
格式化时间
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss")
System.out.println(format.format(cal.getTime()))
计算两个年份之间的闰年数量
public static boolean isLeapYear(int year){
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ;
}
public static void main(String[] args) {
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(format.format(cal.getTime()));
System.out.println("=======40天后取写真========");
cal.add(Calendar.DATE, 40);
System.out.println(format.format(cal.getTime()));
System.out.println("======闰年个数========");
int count=0;
for(int i=1970;i<2011;i++){
if(isLeapYear(i)){
System.out.println(i+"是闰年");
count++;
}
}
System.out.println("闰年数量:"+count);
}
时间和日期处理的综合练习
public static void sub(){
System.out.println("两个日期为:2014-3-8和2017-8-17");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
try {
Date date1= format.parse("2014-3-8");
Date date = format.parse("2017-8-17");
long day =(date.getTime()-date1.getTime())/(1000*60*60*24);
System.out.println("相差天数:"+day);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void change(){
Scanner sc= new Scanner(System.in);
Date date = new Date();
System.out.println("请输入一个毫秒数:");
Long time = sc.nextLong();
Date d = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("转换成日期:"+sdf.format(d));
}
public static void today(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("今天日期:"+sdf.format(date));
}
public static void subAdd(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println("当前的日期:"+sdf.format(date));
cal.add(Calendar.DATE, 10);
System.out.println("加了10天后的日期:"+sdf.format(cal.getTime()));
cal.add(Calendar.DATE, -8);
System.out.println("减去8天后的日期:"+sdf.format(cal.getTime()));
}
public static void addSub(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
Scanner sc = new Scanner(System.in);
System.out.println("请输入指定日期:");
try {
String str = sc.next();
Date date1= format.parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, 10);
System.out.println("加了10天后的日期:"+format.format(cal.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void sdf(){
SimpleDateFormat format = new SimpleDateFormat("北京时间:yy年MM月dd日,为您报时:HH:mm:ss");
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
System.out.println(format.format(cal.getTime()));
}
public static void main(String[] args) {
System.out.println("===========返回两个日期之间相差的天数================");
sub();
System.out.println("===========毫秒转日期================");
change();
System.out.println("===========返回今天的日期=============");
today();
System.out.println("===========返回当前日期加减后的日期=============");
subAdd();
System.out.println("===========返回指定日期的加减天数=============");
addSub();
System.out.println("===========日期格式化=============");
sdf();
}