Convert java.util.date default format to Timestamp in Java

Example – 1
java.util.Date date= new java.util.Date();
Timestamp ts_now = new Timestamp(date.getTime());

Example – 2
SimpleDateFormat sdf = new SimpleDateFormat(“EEE MMM dd kk:mm:ss z yyyy”);
Date d = sdf.parse(“Mon May 27 11:46:15 IST 2013″);
Calendar c = Calendar.getInstance();
c.setTime(d);
long time = c.getTimeInMillis();
long curr = System.currentTimeMillis();
long diff = curr – time; //Time difference in milliseconds

Example – 3
String str_date=month+”-“+day+”-“+yr;
DateFormat formatter = new SimpleDateFormat(“MM-dd-yyyy”);
Date date = (Date)formatter.parse(str_date);
long output=date.getTime()/1000L;
String str=Long.toString(output);
long timestamp = Long.parseLong(str) * 1000;

Example – 4
long startTime = date.getTime() * 1000000;;
long estimatedTime = System.nanoTime() – startTime;

Example – 5
You can use DateFormat(java.text.*) to parse the date:
DateFormat df = new SimpleDateFormat(“EEE MMM dd kk:mm:ss z yyyy”, Locale.ENGLISH);
Date d = df.parse(“Mon May 27 11:46:15 IST 2013”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s