import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
*
* @author R.Amirtharaj
*/
public class TimestampDemo {
public static void main(String arg[])
{
try {
long ct = System.currentTimeMillis();
Timestamp ts = new Timestamp(ct);
System.out.println("starttime =" + getStartTimeoftheDay(ts));
// New timestamp
Timestamp nts = getStartTimeoftheDay(ts);
// New Timestamp + 10 mins
Timestamp n10ts = getNextTimestamp(nts, 10);
System.out.println("n10ts =" + n10ts);
Timestamp n10lessts = getPrevTimestamp(nts, 10);
System.out.println("n10lessts =" + n10lessts);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Timestamp getStartTimeoftheDay(Timestamp ts)
{
String fts = null;
Timestamp fts1 = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
fts = formatter.format(ts);
fts1 = new Timestamp(formatter.parse(fts).getTime());
//System.out.println("fts = "+fts);
} catch (Exception ex) {
}
return fts1;
}
public static Timestamp getPrevTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()-(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}
public static Timestamp getNextTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()+(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}
// For Geeting Oracle Dateformat
public static String getOracleDateFormat(Timestamp ts)
{
String fts = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
fts = formatter.format(ts);
//fts = new Timestamp(formattedcurTime);
//System.out.println("fts = "+fts);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
public static Timestamp getRounded15Mins(Timestamp ts)
{
long ct = ts.getTime();
long diff = ct % (1000*60*15);
long nct = ct- diff;
Timestamp fts = new Timestamp(nct);
return fts;
}
public static Date getDateString(String str) {
Date fts = null;
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
fts = formatter.parse(str);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
public static Date addHoures(String str, int nofHrs)
{
Date fts = null;
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
fts = formatter.parse(str);
long ols = fts.getTime()+(nofHrs*60*60*1000);
fts = new Date(ols);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
}
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
*
* @author R.Amirtharaj
*/
public class TimestampDemo {
public static void main(String arg[])
{
try {
long ct = System.currentTimeMillis();
Timestamp ts = new Timestamp(ct);
System.out.println("starttime =" + getStartTimeoftheDay(ts));
// New timestamp
Timestamp nts = getStartTimeoftheDay(ts);
// New Timestamp + 10 mins
Timestamp n10ts = getNextTimestamp(nts, 10);
System.out.println("n10ts =" + n10ts);
Timestamp n10lessts = getPrevTimestamp(nts, 10);
System.out.println("n10lessts =" + n10lessts);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Timestamp getStartTimeoftheDay(Timestamp ts)
{
String fts = null;
Timestamp fts1 = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
fts = formatter.format(ts);
fts1 = new Timestamp(formatter.parse(fts).getTime());
//System.out.println("fts = "+fts);
} catch (Exception ex) {
}
return fts1;
}
public static Timestamp getPrevTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()-(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}
public static Timestamp getNextTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()+(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}
// For Geeting Oracle Dateformat
public static String getOracleDateFormat(Timestamp ts)
{
String fts = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
fts = formatter.format(ts);
//fts = new Timestamp(formattedcurTime);
//System.out.println("fts = "+fts);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
public static Timestamp getRounded15Mins(Timestamp ts)
{
long ct = ts.getTime();
long diff = ct % (1000*60*15);
long nct = ct- diff;
Timestamp fts = new Timestamp(nct);
return fts;
}
public static Date getDateString(String str) {
Date fts = null;
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
fts = formatter.parse(str);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
public static Date addHoures(String str, int nofHrs)
{
Date fts = null;
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
fts = formatter.parse(str);
long ols = fts.getTime()+(nofHrs*60*60*1000);
fts = new Date(ols);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}
}