We help IT Professionals succeed at work.

how to write a date to a string

jjacksn
jjacksn asked
on
Medium Priority
261 Views
Last Modified: 2008-02-26
I'm trying to read/write a date to a string via the following method:

      private static DateFormat gmtFormat;                   
      static {
            gmtFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
            gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
      }

and then later

writeChildElement(elt, "TimeStamp", gmtFormat.format(timeStamp));
mde.setTimeStamp(gmtFormat.parse(selectedElt.getText()));

But, it looks like the lower order numbers or removed from the value when I watch the Date object in the debugger.  Is there a string representation that is human readable that i can use?
Comment
Watch Question

x4u
Commented:
DateFormat.getDateInstance() does not format the time part of the date, you need to use DateFormat.getDateTimeInstance().

gmtFormat = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL, Locale.US);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
You can use SimpleDateFormat to format it in whatever formnat you want
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> mde.setTimeStamp(gmtFormat.parse(selectedElt.getText()));

Why aren't you just using the original Date object instead of formatting and parsing back.
Manikandan ThiagarajanSenior consultant

Commented:

Author

Commented:
objects, not sure what you mean...?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you're talkikng a date and formatting it as a string then back to a date again.
Why can't you just use the original date instance instead of parsing the string?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.