how to get yydddsssssmmmm format from Current Data base Timestamp in DB2

Hi,

My requirement is to generate unique sequence number with the following format

i.e. yydddsssssmmmm

yy = year
ddd = julian date
sssss = current seconds
mmmm = milliseconds

I am able to do first 2 things. How about the rest.

Thanks
Who is Participating?

Software engineerCommented:
>>sssss - Seconds past midnight (0 - 86399).
That's maths: sssss = hours * 3600 + minutes * 60 + seconds

>>mmmm - milli seconds past the second
Think you mean
mmm - milli seconds past the second (0-999)

That's:
SimpleDateFormat fmt = new SimpleDateFormat("SSS");
String millis = fmt.format( new Date() );
0

Software engineerCommented:
what do you mean with "current seconds"? (and it can be 5 digits long????)
You don't mean the seconds part of the current time, do you?
0

Commented:
and the same question for miliseconds ;-)
0

Software engineerCommented:
Guess you mean yydddssmmm.

Then you become the last part by

SimpleDateFormat fmt = new SimpleDateFormat("ssSSS");
String lastPart = fmt.format( new Date() );

0

Software engineerCommented:
...assuming you DO mean the (milli)seconds part of the current time
;°)
0

Author Commented:
i mean
sssss - Seconds past midnight (0 - 86399).
mmmm - milli seconds past the second

0

Software engineerCommented:

Calendar cal = Calendar.getInstance();
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
0

Author Commented:
This is fine, how do i get Calendar from java.sql.Timestamp?
Because after i got DB Current Timestamp, i have it in java.sql.Timestamp object.
0

Author Commented:
I mean without loosing nanos precision.
We can use set(int year, int month, int date, int hour, int minute, int second)  method of Calendar.
But i loose nono second precision.
0

Commented:
Try the following
TimeStamp t;
...Get the data from DB...
Calendar dt=Calandar.getInstance();
dt.setDate(t);

SimpleDateFormat fmt = new SimpleDateFormat("ssSSS");
String lastPart = fmt.format(dt.getTime());
lastPart=lastPart+t.getNanos();
0

Software engineerCommented:
Thanks for accepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.