Solved

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

Posted on 2004-09-02
11
319 Views
Last Modified: 2008-01-09
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
Comment
Question by:mvkraju
11 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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

by:TimYates
Comment Utility
and the same question for miliseconds ;-)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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

by:zzynx
Comment Utility
...assuming you DO mean the (milli)seconds part of the current time
;°)
0
 

Author Comment

by:mvkraju
Comment Utility
i mean
sssss - Seconds past midnight (0 - 86399).
mmmm - milli seconds past the second

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
Comment Utility
>>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

by:zzynx
Comment Utility

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

by:mvkraju
Comment Utility
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

by:mvkraju
Comment Utility
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

by:MogalManic
Comment Utility
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

by:zzynx
Comment Utility
Thanks for accepting
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now