Java Timestamp Create getTimestampMinusDays(Integer days)

Here you can find the source of getTimestampMinusDays(Integer days)

Description

Return a sql timestamp minus for the current day minus the specified days

License

Open Source License

Parameter

Parameter Description
days a parameter

Declaration

public static Timestamp getTimestampMinusDays(Integer days) 

Method Source Code

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

import java.sql.Timestamp;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from   ww w .j  a v  a 2s. c  o  m
     * Return a sql timestamp minus for the current day minus the specified days
     *
     * @param days
     * @return
     */
    public static Timestamp getTimestampMinusDays(Integer days) {
        Date d = getDateMinusDays(days);
        if (d != null) {
            return new Timestamp(d.getTime());
        }

        return null;
    }

    /**
     * Return the current date minus the number of specified days
     *
     * @param days
     * @return
     */
    public static Date getDateMinusDays(Integer days) {
        if (days != null) {
            int total = days > 0 ? days * -1 : days;
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_YEAR, total);

            return cal.getTime();
        }

        return null;
    }
}

Related

  1. getTimestampFromString(String id)
  2. getTimestampFromString(String s)
  3. getTimeStampFromString(String timeStamp)
  4. getTimestampInEnglish(java.sql.Timestamp t)
  5. getTimestampLexical(boolean showSeconds, Locale locale)
  6. getTimestampNullSafe(final Date ts)
  7. getTimestampOrNull(Object dbResult)
  8. getTimeStampString()
  9. getTimestampString(long timeInMillis)