PDA

View Full Version : Converting 2 character dates to 4 char dates using Java


sdjaz
04-24-2003, 07:04 PM
Converting 2 character dates to 4 char dates using Java

I am fairly new to java and I've been searching and searching to find out how to convert the following string date from text file to a string date that has a 4 character year.
Here is how it's stored in the file: 030421 representing April 21, 2003 in YYMMDD format and I want to convert the string to look like this: 2003-04-21 format to insert into an oracle database.
Do you have any sample code or suggestions on how to do this? I've tried everything under the SUN lol , to get this to work to no avail.

I appreciate any assistance you can give me or forwarding to any website that may be able to give me the answers I'm looking for.

Thanks,

Jim Z.

liorean
04-24-2003, 07:13 PM
Can't give you the solution in Java, but if you mean JavaScript, I might be able to hand you the solution:

function fnFormatDate(){
var
datesplit=/(\d{2}){3}/.exec(date),
finaldate=[
(parseInt(datesplit[1])<30?'20':'19')+datesplit[1],
datesplit[2],
datesplit[3]
].join('-');
return finaldate;
}

(BTW, don't cross-post even if it's in private messages. Most people who are likely to answer a PM also keep quite good look on the forums.)

cheesebagpipe
04-24-2003, 10:41 PM
liorean -

Can you nest quantifiers like that? Couldn't get exec() to work with it. This did:

function fnFormatDate(sDate) {
var datesplit = /(\d{2})(\d{2})(\d{2})/.exec(sDate);
return [
(parseInt(datesplit[1]) < 30 ? '20' : '19') +
datesplit[1] , datesplit[2] , datesplit[3]
].join('-');
}

alert(fnFormatDate('030421'));

liorean
04-24-2003, 10:52 PM
I've been working with non-javascript regex for a while, so it might be that those were more advanced...

I guess this is the smallest working regex in JavaScript: /(\d\d)(\d\d)(\d\d)/