• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to Delimit A Number

 
Ranch Hand
Posts: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also you need to check for errors. What should happen if the value returned from the database is 256798 ?
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic