• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Delimit A Number  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From remote data I get a number like 70000 or 4321

This represents a time as hours(1 or 2 digits), minutes(2 digit), seconds(2 digit)

I want to take these numbers and display them as 7:00:00 and 43:21

Can someone help me or point me in the right direction please?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems a simple enough exercise to separate the digits into groups of two, what's the issue that's holding you up?
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Seems a simple enough exercise to separate the digits into groups of two, what's the issue that's holding you up?


Probably so simple I can't see it. You know the old adage can't see forest for trees.

Is there a way to just format 70000 or 4921 into a time format HH:mm:ss?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:From remote data I get a number like 70000 or 4321...

May I ask why? If this is just a classroom exercise, then fine; but if not, there are already established standards out there for passing date/time values, such as ISO 8601, and your "provider" would probably do well to use them.

And you might even get brownie points for suggesting it.

Winston
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Dyke wrote:From remote data I get a number like 70000 or 4321...

May I ask why? If this is just a classroom exercise, then fine; but if not, there are already established standards out there for passing date/time values, such as ISO 8601, and your "provider" would probably do well to use them.

And you might even get brownie points for suggesting it.

Winston


My data is coming from and going into an iSeries(AS400) database the time field for transactions is stored as a max 6 digit number.

I need the user interface to display this field as hours:minutes(not concerned about seconds). The user will also be entering the data as 24-Hour time format.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:Probably so simple I can't see it. You know the old adage can't see forest for trees.
Is there a way to just format 70000 or 4921 into a time format HH:mm:ss?

Well, take one of them to start with: 4921.

First question: Does your "format" allow that to be hh:mm, or does it have to be mm:ss? Because if it's the first, it probably won't work.

Which is just another reason to use existing standards.

But let's suppose it is mm:ss - how would YOU break it up?

Hint: Imagine it's the number 4,921. How would you get (a) the '49' and (b) the '21' out of that?

It really is very simple; and you'll be much happier if you work it out for yourself.

Winston
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Dyke wrote:Probably so simple I can't see it. You know the old adage can't see forest for trees.
Is there a way to just format 70000 or 4921 into a time format HH:mm:ss?

Well, take one of them to start with: 4921.

First question: Does your "format" allow that to be hh:mm, or does it have to be mm:ss? Because if it's the first, it probably won't work.

Which is just another reason to use existing standards.

But let's suppose it is mm:ss - how would YOU break it up?

Hint: Imagine it's the number 4,921. How would you get (a) the '49' and (b) the '21' out of that?

It really is very simple; and you'll be much happier if you work it out for yourself.

Winston


The max 6 digit number will have to work from right to left due to the fact the hour portion can be 1 or two digits. 4921 is 49 minutes, 21 seconds while 70000 is 7 hours, 0 minutes, 0 seconds
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:My data is coming from and going into an iSeries(AS400) database the time field for transactions is stored as a max 6 digit number.

That seems highly arcane - which makes me wonder if your "provider" isn't just being lazy.

It seems highly unlikely to me that ANY database would store a time internally as a six-digit number already converted to [hh]mmss, because that's a string format.

Another question: Is this an SQL database (eg, DB2)? Because if so, you can almost certainly import it as a java.sql.Date.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:The max 6 digit number will have to work from right to left due to the fact the hour portion can be 1 or two digits. 4921 is 49 minutes, 21 seconds while 70000 is 7 hours, 0 minutes, 0 seconds

OK, so assuming this is a number and not a string, how would you get 49 from 4,921?

Winston
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Dyke wrote:The max 6 digit number will have to work from right to left due to the fact the hour portion can be 1 or two digits. 4921 is 49 minutes, 21 seconds while 70000 is 7 hours, 0 minutes, 0 seconds

OK, so assuming this is a number and not a string, how would you get 49 from 4,921?

Winston


If it was a string I would use substring method. But I know this will not work on a number. Do I convert to number first or are there methods like such for numbers?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:If it was a string I would use substring method. But I know this will not work on a number. Do I convert to number first or are there methods like such for numbers?

You're overthinking this. It's a number. So how do you get the last two decimal digits of a number? Or indeed, the ones before the last two digits?

How do you know if a number contains more than 4 decimal digits?

Forget 4921 and try 7,694. How would you do it then?

Winston
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Dyke wrote:If it was a string I would use substring method. But I know this will not work on a number. Do I convert to number first or are there methods like such for numbers?

You're overthinking this. It's a number. So how do you get the last two decimal digits of a number? Or indeed, the ones before the last two digits?

How do you know if a number contains more than 4 decimal digits?

Forget 4921 and try 7,694. How would you do it then?

Winston


This what I came up with and it works. But is the correct way?

timein = 124213, timeout = 145701

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I'd flatten the value to string first and divide it up into 2-character chunks rather than dealing with it as a number. Obviously, your code would need to be able to correctly deal with strings with an odd number of digits.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:

If your getTimein() really returns a float, I would cast it as an int, declare variable 'timein' as an int, and then perform all the calculations in lines 4-10 in integer arithmetic.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Another question: Is this an SQL database (eg, DB2)? Because if so, you can almost certainly import it as a java.sql.Date.


It's true that the AS/400 databases do support date and time and timestamp columns, just like any other SQL database you'd encounter today. But that doesn't mean that the database designers actually have to use those column types. It's possible that the database designer used 6-digit integer columns for those time values, and the older the database is, the more likely it is that they did that. The original version of the AS/400 database back in the 80's didn't support those column types, they were only implemented in the 90's (if my memory going back that far is accurate, which it often isn't). And database designers being the conservative types that they are, a lot of AS/400 databases were left unchanged after that time, and even new databases designed after that time often failed to use those column types. Even when the Year 2000 projects forced a lot of database changes (due to using integer column types for dates) they generally were still left unchanged. I know that was the case in the company where I worked, where our databases were designed in the 80's.

Of course I don't know whether the database in this post has that problem, but it's certainly not unlikely.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:This what I came up with and it works. But is the correct way?

Pretty much, but in every case you only need the last two digits, so what about:Or you could do as Bear suggested and extract them from a string. But whatever you do:
try to get into the habit of writing methods to do things for you.

Winston
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you need to check for errors. What should happen if the value returned from the database is 256798 ?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!