• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I extract date (from String) to number  RSS feed

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can I change this date pattern 4 Years and 2 Months as 4.2. I am trying to convert date from string to integer.

"Years and" "Months" are always there. If we replace Years and as dot by removing months and white spaces then can be correct results

Please advise

Thanks in anticipation
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend a regular expression.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you kindly give me an example
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this and problem solved but can I make it better

 
Teo Filimon
Greenhorn
Posts: 15
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By better do you mean faster or simpler?

I'm not sure how much simpler you can go than something like the lines you mentioned without sacrificing code readability. If you care about performance, perhaps looking for space characters with indexOf(char) and indexOf(char,start) and then using substring(start,end) would be slightly faster, e.g.

int firstSpace = x.indexOf(' ');
int secondSpace = x.indexOf(' ', firstSpace+1);
x = substring(0,firstSpace).concat(x.substring(secondSpace, x.indexOf(' ', secondSpace+1)));
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:Can I change this date pattern 4 Years and 2 Months as 4.2. I am trying to convert date from string to integer.

"Years and" "Months" are always there. If we replace Years and as dot by removing months and white spaces then can be correct results

"4.2" is not an integer, but a floating point.

also, how will you distinguish between "4 years, 1 months" and "4 years, 10 months"? Both become 4.1 when converted to a number.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:
Can I change this date pattern 4 Years and 2 Months as 4.2. I am trying to convert date from string to integer.

"Years and" "Months" are always there. If we replace Years and as dot by removing months and white spaces then can be correct results


What about the difference between 4 Years and 1 Month and 4 Years and 10 Months ?? The integers 4.1 and 4.10 are equal, so you won't be able to tell those two dates apart.

Henry
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am removing only "Years and" and Months then removing spaces so both dates will work. isn't right?
 
Teo Filimon
Greenhorn
Posts: 15
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to do with this number that you obtain?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that depends...do you want to convert it to a numeric type that holds a numeric value, or do you want to create a new string with two sets of digits, separated by a decimal point?

These are two very different things. Writing proper specifications is very important. The details matter. If you say "I want to convert this to an integer", the word "integer" has a very specific meaning to computer programmers (and mathematicians).
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for stating integer instead of float. Actually I am going to store this value in oracle column of type number and then checking experience that search cvs that have experience from 1 year to 4 years.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:Sorry for stating integer instead of float. Actually I am going to store this value in oracle column of type number and then checking experience that search cvs that have experience from 1 year to 4 years.


Regardless, even with float, there is no difference between the float values 4.1 and 4.10. So, there is no difference between 4 Years and 1 Month and 4 Years and 10 Months. This is still an issue that you need to tackle.

Henry
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be no difference when I am getting as float but here I am getting as string and inserting into number type column of oracle sql. so its work fine for me
 
Teo Filimon
Greenhorn
Posts: 15
Android Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest i would devise a formula which in the end would result in milliseconds (long in Java) and i would store this in the database.

years*MILLIS_PER_YEAR+months*MILLIS_PER_MONTH+day*MILLIS_PER_DAY

(be careful with the months, days and years, whether you want them 0-based or not)

You can then obtain anything from this formula by using division and modulo.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:may be no difference when I am getting as float but here I am getting as string and inserting into number type column of oracle sql. so its work fine for me

be prepared for the wrath of some other coder when THEY have to look at/debug/fix your code. I have no idea what an Oracle DB will do when you insert a string into a number column, but i'm pretty sure that I wouldn't rely on it to do the right thing.

IF you insist on going down this path, I would suggest you test the HECK out of it. I would develop a ton of test cases that my guess will fail. For example, Insert someone with 4 years and 10 months. Then pull out everyone with more than 4 years and 6 months experience, and I would bet almost anything you don't get this person.

insert someone with 4 years and 2 months experience, and someone with 4 years, 11 months. Pull them out and sort by experience. I would bet you it says the person with 4y 2m has MORE experience than someone with 4y 11m.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. How I tackle it?

Best regards
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!