Java SQL Date Add add(Date date, int unit, int amount)

Here you can find the source of add(Date date, int unit, int amount)

Description

add

License

Open Source License

Declaration

private static Date add(Date date, int unit, int amount) 

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 {
    private static Date add(Date date, int unit, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);/* w w  w  .j a va2s  .c o  m*/
        c.add(unit, amount);
        return c.getTime();
    }

    private static Timestamp add(Timestamp timestamp, int unit, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(timestamp);
        c.add(unit, amount);
        Timestamp ts = new Timestamp(c.getTimeInMillis());
        ts.setNanos(ts.getNanos() + timestamp.getNanos() % 1000000);
        return ts;
    }

    public static int getNanos(Timestamp timestamp) {
        return timestamp.getNanos();
    }
}

Related

  1. addDate(int add, java.util.Date d)
  2. addDate(java.util.Date date, int day)
  3. addDate(String date)
  4. addDays(Date date, int days)