[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

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

Posted on 2004-09-02
Medium Priority
360 Views
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
0
Question by:mvkraju
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 37

Expert Comment

ID: 11964461
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

LVL 35

Expert Comment

ID: 11964516
and the same question for miliseconds ;-)
0

LVL 37

Expert Comment

ID: 11964532
Guess you mean yydddssmmm.

Then you become the last part by

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

0

LVL 37

Expert Comment

ID: 11964541
...assuming you DO mean the (milli)seconds part of the current time
;Â°)
0

Author Comment

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

0

LVL 37

Accepted Solution

zzynx earned 150 total points
ID: 11964645
>>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

LVL 37

Expert Comment

ID: 11964683

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 Comment

ID: 11965688
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 Comment

ID: 11965734
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

LVL 21

Expert Comment

ID: 11965877
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

LVL 37

Expert Comment

ID: 11970570
Thanks for accepting
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula wâ€¦
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll