52

I am trying to insert java.util.Date after converting it to java.sql.Timestamp and I am using the following snippet:

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());

But this is giving me sq as 2014-04-04 13:30:17.533

Is there any way to get the output without milliseconds?

Shitu
  • 821
  • 5
  • 12
  • 20
  • 4
    Well you're currently creating a `java.sql.Timestamp` - did you want that, or a `java.sql.Date`? They're different types... – Jon Skeet Apr 04 '14 at 08:06
  • 1
    Use a Calendar, set the millisecond to 0, the create your Timestamp from the calendar's time. – JB Nizet Apr 04 '14 at 08:07
  • @JonSkeet, Date will not be able to hold time information like Hours Mins – RaceBase Apr 04 '14 at 08:09
  • @Reddy "The class Date represents a specific instant in time, with millisecond precision. " – Gyro Gearless Apr 04 '14 at 08:15
  • 1
    @Reddy: Yes, but I was going from the first line of the post: "I want to convert java.util.Date to java.sql.date" - basically the requirements are entirely unclear at the moment. – Jon Skeet Apr 04 '14 at 08:16
  • @JonSkeet, pretty much he's trying to store the data in database and it's storing with milliseconds, which he doesn't want. since util.Date has to be converted to sql.Date commonly to store into DB – RaceBase Apr 04 '14 at 08:19
  • I want something in this format: 2014-04-04 13:30:17 – Shitu Apr 04 '14 at 08:21
  • 2
    @Shitu: Right, so you don't want `java.sql.Date` at all. Please edit your post. – Jon Skeet Apr 04 '14 at 08:23
  • 2
    @Reddy: You still seem to be missing my point: the original post is unclear about whether the time part is required at all, given that it starts off talking about `java.sql.Date` and then uses `java.sql.Timestamp`. I'm well aware of both types, but they're not the same, and we weren't given actual requirements... with the comment a couple of minutes ago, it's clearer - but that information had to come from the OP... – Jon Skeet Apr 04 '14 at 08:24
  • 1
    @Reddy: Additionally, are you actually concerned about what gets stored, or just the output when you convert the value to text later? – Jon Skeet Apr 04 '14 at 08:26
  • 1
    In the context of storing something in a `datetime` column in a database, I don't understand the requirement "`I want something in this format`". Format has to do with output, not data types. – Stewart Apr 04 '14 at 08:41

5 Answers5

48

You can cut off the milliseconds using a Calendar:

java.util.Date utilDate = new java.util.Date();
Calendar cal = Calendar.getInstance();
cal.setTime(utilDate);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new java.sql.Timestamp(utilDate.getTime()));
System.out.println(new java.sql.Timestamp(cal.getTimeInMillis()));

Output:

2014-04-04 10:10:17.78
2014-04-04 10:10:17.0
janos
  • 120,954
  • 29
  • 226
  • 236
16

Take a look at SimpleDateFormat:

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());  

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(sq));
Markus
  • 1,649
  • 1
  • 22
  • 41
8

The problem is with the way you are printing the Time data

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());
System.out.println(sa); //this will print the milliseconds as the toString() has been written in that format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(timestamp)); //this will print without ms
RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 3
    The OP is interested in storing the data to a database - that doesn't require a text conversion at all. The question is unclear, basically - it's not obvious whether the OP doesn't want to *store* milliseconds or doesn't want to *render* them. – Jon Skeet Apr 04 '14 at 08:25
5

I suggest using DateUtils from apache.commons library.

long millis = DateUtils.truncate(utilDate, Calendar.MILLISECOND).getTime();
java.sql.Timestamp sq = new java.sql.Timestamp(millis );

Edit: Fixed Calendar.MILISECOND to Calendar.MILLISECOND

NullPointer
  • 195
  • 4
  • 18
Miron Balcerzak
  • 886
  • 10
  • 21
1
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("utilDate:" + utilDate);
System.out.println("sqlDate:" + sqlDate);

This gives me the following output:

 utilDate:Fri Apr 04 12:07:37 MSK 2014
 sqlDate:2014-04-04
nikis
  • 11,166
  • 2
  • 35
  • 45