3

I'm trying to set the current datetime in a java EE7 web app to an apache derby database I have.

I'm using timestamp in both java and derby so I don't have to worry about type conversions.

However, the only way I know of to get the current datetime is through calendar.set(Calendar time).

Is there inherent way to set the current datetime for timestamp or will I need to use a converter?

Pynchia
  • 10,147
  • 5
  • 33
  • 41
ICantHandleThis
  • 65
  • 1
  • 11

3 Answers3

6

You can use the Date class for initializing to the current time , i.e :

Timestamp timestamp = new Timestamp(new Date().getTime());

The java.util.Calendar class is an abstract encapsulation of the Date object. Calendar provides getter and setter for the date fields , nothing more. And it comes at a cost.

In your case using a simple Date is ideal.

Laurentiu L.
  • 6,368
  • 1
  • 30
  • 57
1

use joda jar and import the required packages.

DateTime d = new DateTime().withZone(DateTimeZone.forID(TIME_ZONE));
Aleksandr M
  • 23,988
  • 12
  • 67
  • 136
Ejaz Ahmed
  • 338
  • 1
  • 15
0

Use Following code.

Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
isurujay
  • 1,286
  • 10
  • 14