For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2005 > Date and Time conversion









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Date and Time conversion
Rizwan

2005-01-27, 4:03 pm

I have an Integer variable which contains date in YYYYMMDD format. I want to
convert it first to a Date variable and then to a String variable with
format MM/DD/YYYY. How can I do that?

I also have an Integer which contain time in HHMMSS format. I want to
convert it to a String variable with format HH:MM:SS. How can I do that?

TIA



klynn47@comcast.net

2005-01-27, 4:03 pm

One thing I would suggest for the first one (I assume you mean it's
stored in an int variable), I would convert it to a String, and then
left pad with 0s to make sure you have four numbers at the beginning.
Then the year would be substring(0,4), then month would be
substring(4,6), and the day would be substring(6).

If the second form is stored in an int, you could do somethind like

seconds = time % 100
time = time/100
minutes = time % 100
hour = time/100
Then you could concatenate them

hour + ":" + minute + ":" + seconds

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com