Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date format conversion

Status
Not open for further replies.

wwworg

Programmer
Feb 10, 2006
35
CA
Hey guys

I am using tomcat 5.5, J2SE 1.5 with Oracle 8i

Here is a function I have that converts a date like (18-06-2006) to (18-JUN-2006).

<%! public String changeDateFormat(String str) throws java.text.ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date tDate = new java.util.Date();
tDate = df.parse(str);
df = new SimpleDateFormat("dd-MMM-yyyy");
String date1 = df.format(tDate).toUpperCase();
return date1;
}
%>

What I want to be able to do is convert a date like (9999-12-30) to (30-DEC-9999). What Modifications do I need to make to this function to be able to do this. Because curently if I pass (9999-12-30) as a parameter, it gives an error saying "unparseable date".

Any help would be highly appreciated.

Thanks
 
You just have to switch the way that the SimpleDateFormat is constructed.

Instead of using "dd-MM-yyyy", you would use "yyyy-MM-dd" :
Code:
<%! 
public String changeDateFormat(String str) throws java.text.ParseException {
    SimpleDateFormat df = new SimpleDateFormat("[b]yyyy-MM-dd[/b]");
    java.util.Date tDate = new java.util.Date();
    tDate = df.parse(str);
    df = new SimpleDateFormat("dd-MMM-yyyy");
    String date1 = df.format(tDate).toUpperCase();
    return date1;
   }     
%>

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Yes, I understand that, but there will also be some dates in (dd-mm-yyyy) format that I also want to convert to (dd-MON-yyyy). So I believe I need to a way to find out the format of incoming data parameter. So if incoming date is (yy-mm-dddd) then do SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");

and if incoming date is yyyy-mm-dd then do SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

I am just not sure how to find that format?? or there is any other way to do.

Thanks for your response sedj. I will appreciate a little help here.

Thanks
 
Well you will have to work out that bit yourself, probably using regular expressions or something like that. Without actually knowing the rules to which your dates conform, I cannot really help you any further !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Please don't do that - it is a really filthy horrible way of using exception blocks - really, really bad idea IMO.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hey guys

I have tried to do this

if (str.indexOf("-") == 5)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd");
java.util.Date tDate = new java.util.Date();
tDate = df.parse(str);
df = new SimpleDateFormat("dd-MMM-yyyy");
String date1 = df.format(tDate).toUpperCase();
return date1;
}
else
{
SimpleDateFormat df = new SimpleDateFormat("dd-M-yyyy");
java.util.Date tDate = new java.util.Date();
tDate = df.parse(str);
df = new SimpleDateFormat("dd-MMM-yyyy");
String date1 = df.format(tDate).toUpperCase();
return date1;
}

but I am still getting that error "unparseable date". Is there something wrong with this above code??

Thanks guys
 
You can also use the parse method of SimpleDateFormat class. It will return a null value if the format is not the speicifed.

Cheers,
Dian
 
I don't think it will Dian - it'll throw an exception.

If you defintely have only two date formats :

dd-MM-yyyy and yyyy-MM-dd , then I would say this would be fine :

Code:
String date = "2006-02-28";
SimpleDateFormat df = null;if (date != null) {
  String dateParts = date.split("-");
  if (dateParts.length > 1) {
    int firstPartLen = dateParts[0].length();
    if (firstPartLen  == 4) {
       df = new SimpleDateFormat("yyyy-MM-dd");
    } else if(firstPartLen == 2) {
       df = new SimpleDateFormat("dd-MM-yyyy");
    } else {
       df = null;
    }
  }


  if (df == null) {
    // error now - format not recognized !
  } else {
        java.util.Date tDate = new java.util.Date();
        tDate = df.parse(str);
        df = new SimpleDateFormat("dd-MMM-yyyy");
        String date1 = df.format(tDate).toUpperCase();
        return date1;
  }

  return null;
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
formatting gone funny above :

Code:
String date = "2006-02-28";


SimpleDateFormat df = null;

if (date != null) {
  String dateParts = date.split("-");
  if (dateParts.length > 1) {
    int firstPartLen = dateParts[0].length();
    if (firstPartLen  == 4) {
       df = new SimpleDateFormat("yyyy-MM-dd");
    } else if(firstPartLen == 2) {
       df = new SimpleDateFormat("dd-MM-yyyy");
    } else {
       df = null;
    }
  }
}


if (df == null) {
	// error now - format not recognized !
} else {
	java.util.Date tDate = df.parse(str);
	df = new SimpleDateFormat("dd-MMM-yyyy");
	String date1 = df.format(tDate).toUpperCase();
	return date1;
}

  return null;
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Yeah I know Dian - but using exceptions in that way is horrible and messy and just bad practice - think of the stack you'll be creating ! Much better and quicker to handle it programmatically.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Ah Ha !
Quoted from "Core java - Volume 1 - Fundamentals", page 576, published by Sun itself

There is a tendency to overuse exceptions ... exception handling is not supposed to replace a simple test.
The book then goes on to describe, and demonstrate how it is slower to use exceptions than to do a simple test.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I know, so he can use this other method to check wether the format is the correct one and act in consequence.

Cheers,
Dian
 
But that does not seem to return null for an incorrect format (as defined as incorrect by OP).

Eg :

Code:
String date = "01-02-2006";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = df.parse(date, new ParsePosition(0));
System.out.println(d);

Rather than "null", I get "Fri Jul 30 00:00:00 GMT 0006" ...


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hey guys, thanks for an interesting discussion
I used to code submitted by sedj but it is now setting df to null. This very strange

here is the code

<%! public String changeDateFormat(String str) throws java.text.ParseException {
SimpleDateFormat df = null;
if (str != null) {
String [] dateParts = str.split("-");
if (dateParts.length > 1) {
int firstPartLen = dateParts[0].length();
if (firstPartLen == 4) {
df = new SimpleDateFormat("yyyy-MM-dd");
} else if(firstPartLen == 2) {
df = new SimpleDateFormat("dd-M-yyyy");
} else {
df = null;
}
}
}
if (df == null) {
//error now - format not recognized !
return "21-JUL-2008";
} else {
java.util.Date tDate = df.parse(str);
df = new SimpleDateFormat("dd-MMM-yyyy");
String date1 = df.format(tDate).toUpperCase();
return date1;
}
}
%>

Also, can please someone tell me how to put the code inside the code editor here. using <code<</code>????

Thanks guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top