package com.tradingplatform.dto;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 用于操作时间运算的类
* @author cc
*
*/
public class DateOperationDto {
/**
* 得到几天前的时间
* @param d
* @param day
* @return
*/
public Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
/**
* 得到几天后的时间
* @param d
* @param day
* @return
*/
public Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
System.out.println(sdf.format(date));
Date addDate=new Date(date.getTime()+600000);
System.out.println(sdf.format(addDate));
//2017-05-07 10:32:33
String oldTime="2017-05-07 10:35:34";
Date oldDate=sdf.parse(oldTime);
System.out.println(sdf.format(oldDate));
Date newTime=new Date(oldDate.getTime()+600000);
Date newTime1=new Date(oldDate.getTime()-10000);
System.out.println(newTime);
System.out.println(sdf.format(newTime));
System.out.println(sdf.format(newTime1));
}
}