Here you can find the source of calculateTime(Date date, String addpart, int num)
public static Date calculateTime(Date date, String addpart, int num) throws ParseException
//package com.java2s; /**/*from w w w . ja v a 2 s . c o m*/ * Project: szs-demo * * File Created at 2016?8?19? * chiukong * * Copyright 2015 Dafy Finance Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Dafy Finance Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with http://www.dafy.com. */ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { public static String calculateTime(String time, String formatStr, String addpart, int num) throws ParseException { DateFormat dateFormat = new SimpleDateFormat(formatStr); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(dateFormat.parse(time)); if (addpart.equalsIgnoreCase("Y")) { cal.add(Calendar.YEAR, num); } else if (addpart.equalsIgnoreCase("M")) { cal.add(Calendar.MONTH, num); } else if (addpart.equalsIgnoreCase("D")) { cal.add(Calendar.DATE, num); } else if (addpart.equalsIgnoreCase("H")) { cal.add(Calendar.HOUR, num); } else if (addpart.equalsIgnoreCase("F")) { cal.add(Calendar.MINUTE, num); } else if (addpart.equalsIgnoreCase("S")) { cal.add(Calendar.SECOND, num); } return dateFormat.format(cal.getTime()); } public static Date calculateTime(Date date, String addpart, int num) throws ParseException { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); if (addpart.equalsIgnoreCase("Y")) { cal.add(Calendar.YEAR, num); } else if (addpart.equalsIgnoreCase("M")) { cal.add(Calendar.MONTH, num); } else if (addpart.equalsIgnoreCase("D")) { cal.add(Calendar.DATE, num); } else if (addpart.equalsIgnoreCase("H")) { cal.add(Calendar.HOUR, num); } else if (addpart.equalsIgnoreCase("F")) { cal.add(Calendar.MINUTE, num); } else if (addpart.equalsIgnoreCase("S")) { cal.add(Calendar.SECOND, num); } return cal.getTime(); } }