java处理日期时间

时间:2021-02-05 22:06:25

java.util.Calendar

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。 

 

简单示例

// 通过格式化输出日期
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");

Calendar cal
= Calendar.getInstance();// 取当前日期。
System.out.println("Today is:" + format.format(cal.getTime()));

cal
= Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH,
-1);// 取当前日期的前一天.
System.out.println("yesterday is:" + format.format(cal.getTime()));

cal
= Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH,
+1);// 取当前日期的后一天.
System.out.println("nextday is:" + format.format(cal.getTime()));

或者

java.util.Date today=new java.util.Date();   
java.text.SimpleDateFormat dateFormat
= new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat
= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(
"Today is "+dateFormat.format(today));
System.out.println(
"Now is "+dateTimeFormat.format(today));

 

构造特定时间

java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");   

Calendar calendar
= new GregorianCalendar(2007, 11, 25,0,0,0);
Date date
= calendar.getTime();
System.out.println(
"2007 Christmas is:"+format.format(date));

GregorianCalendar构造方法参数依次为:年,月-1,日,时,分,秒. 

或者

java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");   
java.util.Date date
= format.parse("2007-12-25");
System.out.println(
"2007 Christmas is:"+format.format(date));

 

取日期的每部分

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 second =calendar.get(Calendar.SECOND);

取月份要加1

 

获取当前月份的最大天数

Calendar cal = Calendar.getInstance();   
int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(day);

取当月的最后一天

 

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()));

取当月的第一天

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));

求两个日期之间相隔的天数

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);

一年前的日期

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);

一年后的日期

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小时后的时间 

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小时前的时间 

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()));

 

当前日期的星期一和星期天

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
GregorianCalendar cal
= new GregorianCalendar();


int dayInWeek = cal.get(Calendar.DAY_OF_WEEK);
int offset = 0;
if (dayInWeek == 1) {
// 星期天
offset = 6;
}
else {
// 星期一至星期六
offset = dayInWeek - 2;
}
cal.add(GregorianCalendar.DAY_OF_MONTH,
-offset);
String sday
= dateFormat.format(cal.getTime());
cal.add(GregorianCalendar.DAY_OF_MONTH,
6);
String eday
= dateFormat.format(cal.getTime());

System.out.println(
"这个星期的星期一:" + sday);
System.out.println(
"这个星期的星期天:" + eday);

 

获取当前日期所在的星期属于今年的第几周

GregorianCalendar cal = new GregorianCalendar();
int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println(
"这个星期属于第几周:" + weekOfYear);

 

参考 http://alexfc.iteye.com/blog/363185