Java SQL Date Add addMilliseconds(Date date, int milliseconds)

Here you can find the source of addMilliseconds(Date date, int milliseconds)

Description

add Milliseconds

License

Open Source License

Declaration

public static Date addMilliseconds(Date date, int milliseconds) 

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 {
    public static Date addMilliseconds(Date date, int milliseconds) {
        return add(date, Calendar.MILLISECOND, milliseconds);
    }/*from  ww w. j  av  a2 s . c om*/

    public static Timestamp addMilliseconds(Timestamp timestamp, int milliseconds) {
        return add(timestamp, Calendar.MILLISECOND, milliseconds);
    }

    private static Date add(Date date, int unit, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        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(java.util.Date date, int day)
  2. addDate(String date)
  3. addDays(Date date, int days)
  4. addDaysToSQLDate(java.sql.Date date, int days)
  5. addMesData(Date dtReferencia, int mes)
  6. AddSqlDate(String strDate, int iDays)
  7. addYearsToDate(java.sql.Date date, int years)