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

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

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”)

0
Manimaran Samuthirapandi 10 months 2021-08-05T05:53:41+02:00 0 Answers 12 views 0

Leave an answer

Captcha Click on image to update the captcha .