1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.tradingplatform.dto;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
public class DateOperationDto {
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(); }
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)); 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)); } }
|