Java日期计算

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.GregorianCalendar;

 

public class DateTest {

 

public static void main(String[] args) {

 

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

DateTest test = new DateTest();

//Date

Date currentDate = new Date();

System.out.println("当前日期是:" + df.format(currentDate));

System.out.println("一周后的日期是:" + df.format(test.nextWeek(currentDate)));

System.out.println("一月后的日期是:" + df.format(test.nextMonth(currentDate)));

System.out.println("一年后的日期是:" + df.format(test.nextYear(currentDate)));

//Timestamp

Timestamp currentTime = new Timestamp(System.currentTimeMillis());

System.out.println("当前日期是:" + df.format(currentTime));

System.out.println("一周后的日期是:" + df.format(test.nextWeek(currentTime)));

System.out.println("一月后的日期是:" + df.format(test.nextMonth(currentTime)));

System.out.println("一年后的日期是:" + df.format(test.nextYear(currentTime)));

 

//另一种计算方式,这种方式计算月和年的日期比较困难

Timestamp nextTime = new Timestamp(currentTime.getTime() + 7 * 24 * 60 * 60 * 1000);

System.out.println("当前日期是:" + df.format(currentTime));

System.out.println("一周后的日期是:" + df.format(nextTime));

 

}

 

//获取下一周的日期

public Date nextWeek(Date currentDate) {

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(currentDate);

cal.add(GregorianCalendar.DATE, 7);//在日期上加7天

return cal.getTime();

}

 

//获取本周日的日期

public Date getSunday(Date monday) {

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(monday);

cal.add(GregorianCalendar.DATE, 6);//在日期上加6天

return cal.getTime();

}

 

//获取下一月的日期

public Date nextMonth(Date currentDate) {

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(currentDate);

cal.add(GregorianCalendar.MONTH, 1);//在月份上加1

return cal.getTime();

}

 

//获取下一年的日期

public Date nextYear(Date currentDate) {

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(currentDate);

cal.add(GregorianCalendar.YEAR, 1);//在年上加1

return cal.getTime();

}

//获取当前季度
public int getQuarter(){
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
return month/3;
}
}

你可能感兴趣的:(java)