What's the fastest way to remove the minute, second and nanosecond part of a java.sql.timestamp?

thomaszhwang
thomaszhwang used Ask the Experts™
on
What's the fastest way to remove the minute, second and nanosecond part of a java.sql.timestamp?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Check this out

import java.sql.*;

public class TestDate{


	public static void main(String args[]){


		Timestamp ts = new Timestamp(System.currentTimeMillis());
		System.out.println("TimeStamp : "+ts.toString());

		Date dt = new Date(ts.getTime());
		System.out.println("Date : "+dt.toString());

	}

}

Open in new window

Author

Commented:
Actually I still need the hour...

Commented:
import java.sql.*;
import java.util.Calendar;

public class TestDate {

    public static void main(String args[]) {
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            System.out.println("TimeStamp : " + ts.toString());

            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(ts.getTime());
            System.out.println("Date : " + cal.get(Calendar.YEAR)+"-"+ (cal.get(Calendar.MONTH)+1)+"-"+ cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY));

    }
}

Open in new window

Top Expert 2016
Commented:
Try


	final long HOURS = 1000*60*60;
	long time = timestamp.getTime();
	timestamp = new Timestamp(time/HOURS*HOURS);
	System.out.println(timestamp);

Open in new window

Author

Commented:
Thanks.
Top Expert 2016

Commented:
:)

Author

Commented:
I just opened another time calculation question, I would appreciate if you can help as well.  Thanks.

http://www.experts-exchange.com/Programming/Languages/Java/Q_27684413.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial