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,996
  • 5
  • 34
  • 43
ICantHandleThis
  • 65
  • 2
  • 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,566
  • 1
  • 34
  • 60
1

use joda jar and import the required packages.

DateTime d = new DateTime().withZone(DateTimeZone.forID(TIME_ZONE));
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Ejaz Ahmed
  • 339
  • 1
  • 15
0

Use Following code.

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