package com.hpli.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimeUtls {
public static int PLUS = 1;
public static int MINUS = -1;
public final static int YEAR = GregorianCalendar.YEAR;
public final static int MONTH = GregorianCalendar.MONTH;
public final static int DAY_OF_MONTH = GregorianCalendar.DAY_OF_MONTH;
public final static int HOUR_OF_DAY = GregorianCalendar.HOUR_OF_DAY;
public final static int MINUTE = GregorianCalendar.MINUTE;
public final static int SECOND = GregorianCalendar.SECOND;
/**
* 将时间增加若干年
*/
public static Date getTimePlusYear(Date time, int year) {
return getTimeOperation(time, PLUS, YEAR, year);
}
/**
* 将时间减少若干年
*/
public static Date getTimeMinusYear(Date time, int year) {
return getTimeOperation(time, PLUS, MINUS, year);
}
/**
* 将时间增加若干月
*/
public static Date getTimePlusMonth(Date time, int month) {
return getTimeOperation(time, PLUS, MONTH, month);
}
/**
* 将时间减少若干月
*/
public static Date getTimeMinusMonth(Date time, int month) {
return getTimeOperation(time, PLUS, MONTH, month);
}
/**
* 将时间增加若干日
*/
public static Date getTimePlusDay(Date time, int day) {
return getTimeOperation(time, PLUS, DAY_OF_MONTH, day);
}
/**
* 将时间减少若干日
*/
public static Date getTimeMinusDay(Date time, int day) {
return getTimeOperation(time, PLUS, DAY_OF_MONTH, day);
}
/**
* 将时间增加若干时
*/
public static Date getTimePlusHour(Date time, int hour) {
return getTimeOperation(time, PLUS, HOUR_OF_DAY, hour);
}
/**
* 将时间减少若干时
*/
public static Date getTimeMinusHour(Date time, int hour) {
return getTimeOperation(time, PLUS, HOUR_OF_DAY, hour);
}
/**
* 将时间增加若干分
*/
public static Date getTimePlusMinute(Date time, int minute) {
return getTimeOperation(time, PLUS, MINUTE, minute);
}
/**
* 将时间减少若干分
*/
public static Date getTimeMinusMinute(Date time, int minute) {
return getTimeOperation(time, PLUS, MINUTE, minute);
}
/**
* 将时间增加若干秒
*/
public static Date getTimePlusSecond(Date time, int second) {
return getTimeOperation(time, PLUS, SECOND, second);
}
/**
* 将时间减少若干分
*/
public static Date getTimeMinusSecond(Date time, int second) {
return getTimeOperation(time, PLUS, SECOND, second);
}
/**
* 当前时间
*
* @return
*/
public static Date getCurrentTime() {
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
return date;
}
/**
* 时间核心运算函数
*
* @param time
* 原始时间
* @param operationType
* 操作类型,增加或减少
* @param field
* 时间域:年、月、日、时、分、秒
* @param value
* 增加值
* @return 运算后的时间
*/
public static Date getTimeOperation(Date time, int operationType,
int field, int value) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(time);
int newValue = 0;
if (operationType == PLUS) {
newValue = calendar.get(field) + value;
} else if (operationType == MINUS) {
newValue = calendar.get(field) - value;
}
calendar.set(field, newValue);
return calendar.getTime();
}
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSSZ");
Date year = getTimePlusYear(getCurrentTime(), 3);
System.err.println("year: "+dateFormat.format(year));
Date month = getTimePlusMonth(getCurrentTime(), 3);
System.err.println("month: "+dateFormat.format(month));
Date Day = getTimePlusDay(getCurrentTime(), 3);
System.err.println("Day: "+dateFormat.format(Day));
Date Hour = getTimePlusHour(getCurrentTime(), 3);
System.err.println("Hour: "+dateFormat.format(Hour));
Date Minute = getTimePlusMinute(getCurrentTime(), 3);
System.err.println("Minute: "+dateFormat.format(Minute));
Date Second = getTimePlusSecond(getCurrentTime(), 3);
System.err.println("Second: "+dateFormat.format(Second));
}
}