Java SQL Time Calculate addDateTime(String date, String type, int into)

Here you can find the source of addDateTime(String date, String type, int into)

Description

add Date Time

License

Apache License

Declaration

public static String addDateTime(String date, String type, int into) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.GregorianCalendar;

public class Main {

    public static String addDateTime(String date, String type, int into) {
        date = date.replaceAll("-", "/");
        GregorianCalendar grc = new GregorianCalendar();
        grc.setTime(new java.util.Date(date));
        if (type.equals("D")) {
            grc.add(GregorianCalendar.DATE, into);
        } else if (type.equals("M")) {
            grc.add(GregorianCalendar.MONTH, into);
        } else if (type.equals("Y")) {
            grc.add(GregorianCalendar.YEAR, into);
        } else if (type.equals("HH")) {
            grc.add(GregorianCalendar.HOUR, into);
        } else if (type.equals("MI")) {
            grc.add(GregorianCalendar.MINUTE, into);
        } else if (type.equals("SS")) {
            grc.add(GregorianCalendar.SECOND, into);
        }//from  w  w w .  ja  v  a2 s  . c o  m

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String Sdate = new String(formatter.format(grc.getTime()));
        return Sdate;
    }

    public static Timestamp addDateTime(Timestamp date, String type, int into) {
        GregorianCalendar grc = new GregorianCalendar();
        grc.setTime(new java.util.Date(date.getTime()));
        if (type.equals("D")) {
            grc.add(GregorianCalendar.DATE, into);
        } else if (type.equals("M")) {
            grc.add(GregorianCalendar.MONTH, into);
        } else if (type.equals("Y")) {
            grc.add(GregorianCalendar.YEAR, into);
        } else if (type.equals("HH")) {
            grc.add(GregorianCalendar.HOUR, into);
        } else if (type.equals("MI")) {
            grc.add(GregorianCalendar.MINUTE, into);
        } else if (type.equals("SS")) {
            grc.add(GregorianCalendar.SECOND, into);
        }
        return new Timestamp(new java.util.Date(grc.getTimeInMillis()).getTime());
    }
}

Related

  1. addTime(String date, String type, int into, String pattern)
  2. addTime(Time time, int minutes)
  3. addTimeToDate(Date dt)
  4. appendTime(StringBuffer sb, Calendar cal, int nanos)