JAVA处理日期时间常用方法:
1.
java.util.Calendar
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
例:
1Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
2cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.
3
4cal.add(Calendar.DAY_OF_MONTH, +1);//取当前日期的后一天.
5
6//通过格式化输出日期
7java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
8
9System.out.println("Today is:"+format.format(Calendar.getInstance().getTime()));
10
11System.out.println("yesterday is:"+format.format(cal.getTime()));
Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.
cal.add(Calendar.DAY_OF_MONTH, +1);//取当前日期的后一天.
//通过格式化输出日期
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today is:"+format.format(Calendar.getInstance().getTime()));
System.out.println("yesterday is:"+format.format(cal.getTime()));
得到2007-12-25日期:
1Calendar calendar = new GregorianCalendar(2007, 11, 25,0,0,0);
2Date date = calendar.getTime();
3System.out.println("2007 Christmas is:"+format.format(date));
Calendar calendar = new GregorianCalendar(2007, 11, 25,0,0,0);
Date date = calendar.getTime();
System.out.println("2007 Christmas is:"+format.format(date));
java月份是从0-11,月份设置时要减1.
GregorianCalendar构造方法参数依次为:年,月-1,日,时,分,秒.
取日期的部分:
1int year =calendar.get(Calendar.YEAR);
2
3int month=calendar.get(Calendar.MONTH)+1;
4
5int day =calendar.get(Calendar.DAY_OF_MONTH);
6
7int hour =calendar.get(Calendar.HOUR_OF_DAY);
8
9int minute =calendar.get(Calendar.MINUTE);
10
11int seconds =calendar.get(Calendar.SECOND);
int 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_OF_DAY);
int minute =calendar.get(Calendar.MINUTE);
int seconds =calendar.get(Calendar.SECOND);
取月份要加1.
判断当前月份的最大天数:
1Calendar cal = Calendar.getInstance();
2int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
3System.out.println(day);
Calendar cal = Calendar.getInstance();
int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(day);
2.
java.util.
Date
1java.util.Date today=new java.util.Date();
2System.out.println("Today is "+formats.format(today));
java.util.Date today=new java.util.Date();
System.out.println("Today is "+formats.format(today));
取当月的第一天:
1java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-01");
2java.util.Date firstDay=new java.util.Date();
3System.out.println("the month first day is "+formats.format(firstDay));
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-01");
java.util.Date firstDay=new java.util.Date();
System.out.println("the month first day is "+formats.format(firstDay));
取当月的最后一天:
1Calendar cal = Calendar.getInstance();
2int maxDay=cals.getActualMaximum(Calendar.DAY_OF_MONTH);
3java.text.Format formatter3=new java.text.SimpleDateFormat("yyyy-MM-"+maxDay);
4System.out.println(formatter3.format(cal.getTime()));
Calendar cal = Calendar.getInstance();
int maxDay=cals.getActualMaximum(Calendar.DAY_OF_MONTH);
java.text.Format formatter3=new java.text.SimpleDateFormat("yyyy-MM-"+maxDay);
System.out.println(formatter3.format(cal.getTime()));
求两个日期之间相隔的天数:
1java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
2java.util.Date beginDate= format.parse("2007-12-24");
3java.util.Date endDate= format.parse("2007-12-25");
4long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
5System.out.println("相隔的天数="+day);
6
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date beginDate= format.parse("2007-12-24");
java.util.Date endDate= format.parse("2007-12-25");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
System.out.println("相隔的天数="+day);
一年前的日期:
1java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
2java.util.Date todayDate=new java.util.Date();
3long beforeTime=(todayDate.getTime()/1000)-60*60*24*365;
4todayDate.setTime(beforeTime*1000);
5String beforeDate=formatter.format(todayDate);
6System.out.println(beforeDate);
java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date todayDate=new java.util.Date();
long beforeTime=(todayDate.getTime()/1000)-60*60*24*365;
todayDate.setTime(beforeTime*1000);
String beforeDate=formatter.format(todayDate);
System.out.println(beforeDate);
一年后的日期:
1java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
2java.util.Date todayDate=new java.util.Date();
3long afterTime=(todayDate.getTime()/1000)+60*60*24*365;
4todayDate.setTime(afterTime*1000);
5String afterDate=formatter.format(todayDate);
6System.out.println(afterDate);
java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date todayDate=new java.util.Date();
long afterTime=(todayDate.getTime()/1000)+60*60*24*365;
todayDate.setTime(afterTime*1000);
String afterDate=formatter.format(todayDate);
System.out.println(afterDate);
求10小时后的时间
1java.util.Calendar Cal=java.util.Calendar.getInstance();
2Cal.setTime(dateOper);
3Cal.add(java.util.Calendar.HOUR_OF_DAY,10);
4System.out.println("date:"+forma.format(Cal.getTime()));
java.util.Calendar Cal=java.util.Calendar.getInstance();
Cal.setTime(dateOper);
Cal.add(java.util.Calendar.HOUR_OF_DAY,10);
System.out.println("date:"+forma.format(Cal.getTime()));
求10小时前的时间
1java.util.Calendar Cal=java.util.Calendar.getInstance();
2Cal.setTime(dateOper);
3Cal.add(java.util.Calendar.HOUR_OF_DAY,-10);
4System.out.println("date:"+forma.format(Cal.getTime()));
5
java.util.Calendar Cal=java.util.Calendar.getInstance();
Cal.setTime(dateOper);
Cal.add(java.util.Calendar.HOUR_OF_DAY,-10);
System.out.println("date:"+forma.format(Cal.getTime()));
3.
java.sql.
Date
继承自
java.util.
Date,是操作数据库用的日期类型
1java.sql.Date sqlDate = new java.sql.Date(java.sql.Date.valueOf("2007-12-25").getTime());
java.sql.Date sqlDate = new java.sql.Date(java.sql.Date.valueOf("2007-12-25").getTime());
日期比较:简单的比较可以以字符串的形式直接比较,也可使用
java.sql.
Date.valueOf("2007-03-08").compareTo(
java.sql.
Date.valueOf("2007-03-18"))方式来比较日期的大小.也可使用
java.util.
Date.after(
java.util.
Date)来比较.
相差时间:
long difference=c2.getTimeInMillis()-c1.getTimeInMillis();
相差天数:long day=difference/(3600*24*1000)
相差小时:long hour=difference/(3600*1000)
相差分钟:long minute=difference/(60*1000)
相差秒: long second=difference/1000
补充:
1DateFormat df=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
2System.out.println(df.format(new Date()));
3Date date = new Date();
4DateFormat shortDate=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
5DateFormat mediumDate =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
6DateFormat longDate =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
7DateFormat fullDate =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
8
9system.out.println(shortDate.format(date));
10System.out.println(mediumDate.format(date));
11System.out.println(longDate.format(date));
12System.out.println(fullDate.format(date));
13
1408-4-15 下午3:24
152008-4-15 15:24:31
162008年4月15日 下午03时24分31秒
172008年4月15日 星期二 下午03时24分31秒CST
18
19
20Calendar c = Calendar.getInstance();
21
22c.add(Calendar.MONTH, 1); // 目前時間加1個月
23System.out.println(df.format(c.getTime()));
24
25c.add(Calendar.HOUR, 3); // 目前時間加3小時
26System.out.println(df.format(c.getTime()));
27
28c.add(Calendar.YEAR, -2); // 目前時間減2年
29System.out.println(df.format(c.getTime()));
30
31c.add(Calendar.DAY_OF_WEEK, 7); // 目前的時間加7天
32System.out.println(df.format(c.getTime()));
1DateFormat df=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
2System.out.println(df.format(new Date()));
3Date date = new Date();
4DateFormat shortDate=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
5DateFormat mediumDate =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
6DateFormat longDate =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
7DateFormat fullDate =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
8
9system.out.println(shortDate.format(date));
10System.out.println(mediumDate.format(date));
11System.out.println(longDate.format(date));
12System.out.println(fullDate.format(date));
13
1408-4-15 下午3:24
152008-4-15 15:24:31
162008年4月15日 下午03时24分31秒
172008年4月15日 星期二 下午03时24分31秒CST
18
19
20Calendar c = Calendar.getInstance();
21
22c.add(Calendar.MONTH, 1); // 目前時間加1個月
23System.out.println(df.format(c.getTime()));
24
25c.add(Calendar.HOUR, 3); // 目前時間加3小時
26System.out.println(df.format(c.getTime()));
27
28c.add(Calendar.YEAR, -2); // 目前時間減2年
29System.out.println(df.format(c.getTime()));
30
31c.add(Calendar.DAY_OF_WEEK, 7); // 目前的時間加7天
32System.out.println(df.format(c.getTime()));
33
34