Java Timestamp addDate(Timestamp date, String type, int into)

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

Description

add Date

License

Open Source License

Declaration

public static Timestamp addDate(Timestamp date, String type, int into) 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private static final String DEF_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_FORMAT_1 = "yyyy/MM/dd";
    private static final String DATE_FORMAT_2 = "yyyyMMdd";
    private static final String DATE_FORMAT_3 = "MM/dd/yy";
    private static final String DATE_FORMAT_4 = "dd.MM.yy";
    private static final String DATE_FORMAT_5 = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_6 = "yyyy-MM-dd HH:mm";
    private static final String DATE_FORMAT_7 = "yyyy:MM:dd HH:mm:ss";
    private static final String DATE_FORMAT_8 = "yyyy:MM:dd HH:mm";
    private static final String DATE_FORMAT_9 = "yyyyMMdd HHmmss";
    private static final String DATE_FORMAT_10 = "yyyyMMdd HHmm";
    private static final String DATE_FORMAT_11 = "dd.MM.yy HH:mm";
    private static final String DATE_FORMAT_12 = "MM/dd/yy hh:mm a";
    private static final String DATE_FORMAT_13 = "yyyy:MM:dd HH:mm:sss";
    private static final String DATE_FORMAT_14 = "HH:mm:ss dd.MM.yyyy";

    public static Timestamp addDate(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);
        }/*from   w w  w .  j  a v  a  2  s . c o  m*/
        return new Timestamp(grc.getTimeInMillis());
    }

    public static String addDate(String datestr, String type, int into) {
        Timestamp ts = addDate(new Timestamp(parseDate(datestr).getTime()), type, into);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new String(formatter.format(ts.getTime()));
    }

    public static Date parseDate(String datastr, String pattern) {
        try {
            DateFormat parser = new SimpleDateFormat(pattern);
            return parser.parse(datastr);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static Date parseDate(String datestr) {
        if (datestr == null || datestr.trim().equals(""))
            return null;
        String[] patterns = new String[15];
        patterns[0] = DEF_DATE_FORMAT;
        patterns[1] = DATE_FORMAT_1;
        patterns[2] = DATE_FORMAT_2;
        patterns[3] = DATE_FORMAT_3;
        patterns[4] = DATE_FORMAT_4;
        patterns[5] = DATE_FORMAT_5;
        patterns[6] = DATE_FORMAT_6;
        patterns[7] = DATE_FORMAT_7;
        patterns[8] = DATE_FORMAT_8;
        patterns[9] = DATE_FORMAT_9;
        patterns[10] = DATE_FORMAT_10;
        patterns[11] = DATE_FORMAT_11;
        patterns[12] = DATE_FORMAT_12;
        patterns[13] = DATE_FORMAT_13;
        patterns[14] = DATE_FORMAT_14;
        for (int i = 0; i < patterns.length; i++) {
            try {
                DateFormat parser = new SimpleDateFormat(patterns[i]);
                return parser.parse(datestr);
            } catch (ParseException ex) {
            }
        }

        return null;
    }
}

Related

  1. add(Timestamp target, int addNum, int addKind)
  2. add(Timestamp ts, int field, int amount)
  3. addDays(Timestamp date, int days)
  4. addDays(Timestamp day, int offset)
  5. addDaysToTimestamp(Timestamp start, int days)
  6. addDaysToTimestamp(Timestamp timestamp, int numDays)