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

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

Description

add Time

License

Apache License

Declaration

public static String addTime(String date, String type, int into, String pattern) 

Method Source Code


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

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 {

    public static String addTime(String date, String type, int into, String pattern) {
        date = date.replaceAll("-", "/");
        GregorianCalendar grc = new GregorianCalendar();
        Date d = parseDate(date);
        if (d == null) {
            d = new Date();
        }/*  www  .j av a2  s. co m*/
        grc.setTime(d);
        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);
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);

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

    public static Date parseDate(String exifDate) {
        if (exifDate == null) {
            return null;
        }
        String patterns[];
        int i;
        patterns = (new String[] { "yyyy:MM:dd HH:mm:ss", "yyyy:MM:dd HH:mm", "yyyy-MM-dd HH:mm:ss",
                "yyyy-MM-dd HH:mm", "dd.MM.yy HH:mm", "yyyyMMdd HHmmss", "yyyyMMdd.HHmmss", "yyyyMMdd HHmm",
                "MM/dd/yy hh:mm a", "HH:mm:ss dd.MM.yyyy", "yyyy:MM:dd", "yyyy-MM-dd", "dd.MM.yy", "yyyyMMdd",
                "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "MM/dd/yy", "yyyy:MM:dd HH:mm:sss" });
        for (i = 0; i < patterns.length; i++) {
            try {
                DateFormat parser = new SimpleDateFormat(patterns[i]);
                return parser.parse(exifDate);
            } catch (ParseException ex) {
            }
        }
        return null;
    }

    public static Date parseDate(String date, String pattern) {
        if (date == null)
            return null;

        try {
            DateFormat parser = new SimpleDateFormat(pattern);
            return parser.parse(date);
        } catch (ParseException ex) {
        }

        return null;
    }

    public static String parseDate(Timestamp timestamp, String pattern) {
        if (timestamp == null)
            return null;

        DateFormat parser = new SimpleDateFormat(pattern);
        return parser.format(timestamp);
    }
}

Related

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