Java Date Add DateAdd(String strDate, int iCount, int iType)

Here you can find the source of DateAdd(String strDate, int iCount, int iType)

Description

Date Add

License

Open Source License

Declaration

public static String DateAdd(String strDate, int iCount, int iType) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static String DateAdd(String strDate, int iCount, int iType) {
        Calendar Cal = parseDateTime(strDate);
        int pType = 0;
        if (iType == 0) {
            pType = 1;//  w w w.  j a  v a  2 s . c om
        } else if (iType == 1) {
            pType = 2;
        } else if (iType == 2) {
            pType = 5;
        } else if (iType == 3) {
            pType = 10;
        } else if (iType == 4) {
            pType = 12;
        } else if (iType == 5) {
            pType = 13;
        }
        Cal.add(pType, iCount);
        SimpleDateFormat sdf = null;
        if (iType <= 2)
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        else
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(Cal.getTime());
        return sDate;
    }

    public static String DateAdd(String strOption, int iDays, String strStartDate) {
        if (!strOption.equals("d"))
            ;
        return strStartDate;
    }

    public static Calendar parseDateTime(String baseDate) {
        Calendar cal = null;
        cal = new GregorianCalendar();
        int yy = Integer.parseInt(baseDate.substring(0, 4));
        int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
        int dd = Integer.parseInt(baseDate.substring(8, 10));
        int hh = 0;
        int mi = 0;
        int ss = 0;
        if (baseDate.length() > 12) {
            hh = Integer.parseInt(baseDate.substring(11, 13));
            mi = Integer.parseInt(baseDate.substring(14, 16));
            ss = Integer.parseInt(baseDate.substring(17, 19));
        }
        cal.set(yy, mm, dd, hh, mi, ss);
        return cal;
    }

    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String min;
        min = dateString.substring(14, 16);
        return min;
    }
}

Related

  1. dateAdd(Date date, int type, int num)
  2. dateAdd(Date date, int val)
  3. dateAdd(final Date baseDateTime, int daysToAdd, boolean chopTime)
  4. dateAdd(int datePart, int amount, Date date)
  5. dateAdd(int interval, Date date, int n)
  6. DateAdd3Days(String _date)
  7. dateAddDay(Date date, int day)
  8. DateAddDays(Date _date, int days)
  9. dateAddDays(Date date, int days)