Java Date Calculate calculateTime(Date date, String addpart, int num)

Here you can find the source of calculateTime(Date date, String addpart, int num)

Description

calculate Time

License

Open Source License

Declaration

public static Date calculateTime(Date date, String addpart, int num) throws ParseException 

Method Source Code

//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();
    }
}

Related

  1. calculateLookbackDate(int lookback)
  2. calculateMonthIn(Date date1, Date date2)
  3. calculateNextSettleDate(Date date, int addMonth)
  4. calculateOffsetFromGMT(Date date)
  5. calculatePastDate(int days)
  6. calculateTime(Date sendTime, String timeZoneID)
  7. calculateTimestamp()
  8. calculateTruncatedTime(final Date dateTimeBegin, final Date dateTimeEnd, final Date begin, final Date end)
  9. calculateTruncatedTimeByDays( final Date dateTimeStart, final Date dateTimeEnd, final Date start, final Date end)