Sunday, 30 June 2013

How to Set TimeStamp from a Date object and change it in #Java ? #JDBC


You can find the current Date using :

import java.sql.Timestamp;
import java.util.Date;
 
public class GetCurrentTimeStamp 
{
    public static void main( String[] args )
    {
  java.util.Date date= new java.util.Date();
  System.out.println(new Timestamp(date.getTime()));
    }
}


you can also set a special date by using below codes.but notice the trike in the month field 

Date februaryTheFirst = new Date(2013,1,1); // equals 2012-02-01
This might explain what you are seeing. If you want to instantiate 2012-01-01 instead, you should do:
Date firstDayOf2012 = new Date(2012,0,1); // this is 2012-01-01
Exactly the same thing happens when dealing with Calendar:
Calendar.getInstance().set(2012,0,1); // 2012-01-01

If you want to change in Days ..

Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -30);
Date dateBefore30Days = cal.getTime();

also u can do it using Date Object:

Date d = new Date();//intialize your date to any date Date dateBefore = new Date(d.getTime() - n * 24 * 3600 * 1000 ); //Subtract n days 

No comments:

Post a Comment