Java Date Formatting

S

Sammy

I have a date in string format 'yyyy-mm-dd'
I want to reformat this to 'dd-MMM-2004' so I can use it in an insert
statement into a Oracle DB.

The code below shows code I have so far. It should take a date and
reformat it as I require. But it doesn't work. It remains formatted in
yyyy-mm-dd!!

Can some one help me please?

Thanks in advance, Sammy


import java.util.*;
import java.lang.*;
import java.text.SimpleDateFormat;
import java.util.Date;


public class magic{
public String UDATE;
public SimpleDateFormat formatter = new
SimpleDateFormat("yyyy/MM/dd");
public SimpleDateFormat formatter2 = new
SimpleDateFormat("dd-MMM-yyyy");
public java.util.Date theDate;
public String retDate;

public magic(String DATE) {

super();

UDATE = DATE;
theDate = null;
retDate = null;

}


public String returnFormattedDate(){
try {
// Parse with a custom format
theDate = this.formatter.parse(this.DrawDate);
retDate = this.formatter2.format(theDate);
} catch (Exception e) {
}
return retDate;
} //close return date

} //end class
 
L

Lee Fesperman

Sammy said:
I have a date in string format 'yyyy-mm-dd'
I want to reformat this to 'dd-MMM-2004' so I can use it in an insert
statement into a Oracle DB.

The code below shows code I have so far. It should take a date and
reformat it as I require. But it doesn't work. It remains formatted in
yyyy-mm-dd!!

Can some one help me please?

Thanks in advance, Sammy

import java.util.*;
import java.lang.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class magic{
public String UDATE;
public SimpleDateFormat formatter = new
SimpleDateFormat("yyyy/MM/dd");
public SimpleDateFormat formatter2 = new
SimpleDateFormat("dd-MMM-yyyy");
public java.util.Date theDate;
public String retDate;

public magic(String DATE) {

super();

UDATE = DATE;
theDate = null;
retDate = null;

}


public String returnFormattedDate(){
try {
// Parse with a custom format
theDate = this.formatter.parse(this.DrawDate);
retDate = this.formatter2.format(theDate);
} catch (Exception e) {
}
return retDate;
} //close return date

} //end class

Your class declaration snippet will not compile. Where is the declaration for
'DrawDate'? Please post actual code (cut & paste).

Also, you should follow Java conventions for names. Class names should be capitalized;
field names should begin with a lowercase letter; all uppercase should only be used for
constants.
 
M

Murray

public String returnFormattedDate(){
try {
// Parse with a custom format
theDate = this.formatter.parse(this.DrawDate);
retDate = this.formatter2.format(theDate);
} catch (Exception e) {
}
return retDate;
} //close return date

} //end class

Well for starters this code doesn't compile, but I assume that's just a typo
when you copied it into your newreader. this.DrawDate doesn't exist .. did
you mean UDATE?

What exactly do you mean by "It remains formatted in yyyy-mm-dd!!"? What is
"it"? Which variable are you expecting to be in the new format? How are you
actually using this class? I ran your code and it works fine as long as the
original date is in the format that you're expecting, otherwise
returnFormattedDate() returns null.

Also, you're catching an exception but not doing anything about it, so the
ParseException will never be seen. Put something in the catch block so you
can at least see when an exception occurs (e.g. e.printStacktrace()).

In your message you say the date is in 'yyyy-mm-dd' format, but your code
uses 'yyyy/MM/dd'. Could this also be a problem?
 
P

P.Hill

Sammy said:
theDate = this.formatter.parse(this.DrawDate);

What is it you are expecting to be in DrawDate?
This variable is undefined in this class.

Otherwise, your use of two formatters is correct.

But where is your code that shows us it doesn't work?

I changed your code to use UDATE instead of Drawdate
then added a main that called everything and it
worked just fine for me.

I would suggest the following:

1. Use standard Java naming.

1.1 Class names should be capitalized; e.g. Magic, not magic
1.2 neither members nor arguments should be in all cap's;
e.g. String date not String DATE
this.udate not this.UDATE

2.0 Creating a class for each date needing conversion is poor
design in most cases. I would suggest a No-argument constructor
and a toMyFormat( String oldFormat ) method. Or a two arg constructor
with the fromFormat and the toFormat as two separate Strings.

3.0 naming a method returnX is either too tedious (many methods return
something) or unconventional. getDateAsMyFormat() would be a more
typical style.

4.0 This is a question for comp.lang.java.help; where more beginner
questions are answered.

I think you are having trouble calling this class, so you'll need to show
us that code. I'll give you a clue, that my main version was all of
three lines including printing out the result, but I could have done it
as one big statement include return..., println and new (not in that order).

HTH,

-Paul
 
J

John B. Matthews

I have a date in string format 'yyyy-mm-dd'
I want to reformat this to 'dd-MMM-2004' so I can use it in
an insert statement into a Oracle DB.

Why not let Oracle do the work? Just tell it the format of the
date you're sending:

insert into <table> values
(..., TO_DATE('2001-09-11','YYYY-MM-DD'), ...);

John
 
R

Roedy Green

Why not let Oracle do the work? Just tell it the format of the
date you're sending:

If you turn dates into timestamp longs, your code will be portable.
If you use any of the date features of the host SQL (other than JDBC
date literals) it won't be.

On the other hand, you can't do ad-hoc queries as easily with binary
timestamps.

Another useful portable date type is days since 1970 stored as a
signed int or short.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top