[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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.
I appreciate your ideas!!!

Thanks
0
mvkraju
Asked:
mvkraju
1 Solution
 
zzynxSoftware 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
 
TimYatesCommented:
and the same question for miliseconds ;-)
0
 
zzynxSoftware engineerCommented:
Guess you mean yydddssmmm.

Then you become the last part by

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zzynxSoftware engineerCommented:
...assuming you DO mean the (milli)seconds part of the current time
;°)
0
 
mvkrajuAuthor Commented:
i mean
sssss - Seconds past midnight (0 - 86399).
mmmm - milli seconds past the second

0
 
zzynxSoftware 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
 
zzynxSoftware 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
 
mvkrajuAuthor 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
 
mvkrajuAuthor 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
 
MogalManicCommented:
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
 
zzynxSoftware engineerCommented:
Thanks for accepting
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now