/*
* 时间表达方式有三种:long、Date、Calendar
*/
long timer=System.currentTimeMillis();
int year=(int) (timer/1000/60/60/24/365)+1970;
System.out.println(year);
//Date date=new Date(timer);
Date date=new Date();
System.out.println(date.getYear()+1900);
System.out.println(date.getMonth());
// 历法:Calendar
// GregorianCalendar():公元纪年;公历
Calendar calendar=new GregorianCalendar();
// calendar.setTime(date);
year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
/*
* long---->Date
*/
date=new Date(timer);
date.setTime(timer);
/*
* Date---->long
*/
timer=date.getTime();
/*
* Date---->Calendar
*/
calendar.setTime(date);
/*
* Calendar---->Date
*/
calendar.getTime();
/*
* yyyy/MM/dd HH:mm:ss 大H24小时格式
* yyyy/MM/dd hh:mm:ss 小h24小时格式
*/
DateFormat fm=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String str=fm.format(date);
System.out.println(str);
SimpleDateFormat fmt=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date1=fmt.parse(str);
str="2013/2/16 10:32:54";
date1=fmt.parse(str);
calendar.setTime(date1);
calendar.add(calendar.MONTH, 6);
calendar.add(calendar.WEEK_OF_YEAR, -2);
calendar.set(calendar.DAY_OF_WEEK, Calendar.SUNDAY);
date1=calendar.getTime();
str=fmt.format(date1);
System.out.println(str);
Java时间处理
最新推荐文章于 2024-07-07 17:46:06 发布