Avatar of TyBay
TyBay

asked on 

Date Conversion

Greetings.  I am working with an API in JAva that returns a Date object.  The Date object is in the below format when it is returned:
Mon Jan 22 04:47:28 PST 2007.

I need to retrieve a number of Date objects and then convert them in to the below ISO format, perhaps creating a string that can then be used in a servlet for presentation:
2007-02-24T08:09:51.

I have successfully created a new Date object and then formatted as seen below:
                    Date today = new Date();
                    String output;
                    SimpleDateFormat formatter = formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    output = formatter.format(today);

Unfortunately, I can’t seem to do below to get the string I need to present in the correct format:
                    String output;
                    SimpleDateFormat formatter = formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    output = formatter.format(MyDateToConvertFormat);

The data from the API is stored in an Oracle database and I not sure what I am missing here.  Do I need to parse the Date object before trying to format in the ISO format?  How do I create a string from the Date object that is converted in ISO?

Many Thanks
Java

Avatar of undefined
Last Comment
sciuriware
Avatar of Ajay-Singh
Ajay-Singh

use this format
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
Avatar of sciuriware
sciuriware

>>> SimpleDateFormat formatter = formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

should be at least:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

You do:

String output = formatter.format(MyDateToConvertFormat);

What is "MyDateToConvertFormat"?  A Date?

Tell us all we need, including what happens, what do you get. It's impossible to understand.

;JOOP!
Avatar of TyBay
TyBay

ASKER

sorry -
MyDateToConvert is a date object that is returned from an API.  The object that is returned is not the ISO format that I need for presentation.  I took the previous suggestion to see if I could reformat using the same pattern that is returned from the API and I get a runtime exception form the API:

com.idiominc.ws.WSRuntimeException
(covers com.idiominc.wssdk.WSRuntimeException
com.idiominc.ws.WSRuntimeException
(covers java.lang.NullPointerException
      at java.util.Calendar.setTime(Calendar.java:902)
      at java.text.SimpleDateFormat.format(SimpleDateFormat.java:782)
      at java.text.SimpleDateFormat.format(SimpleDateFormat.java:775)
Avatar of sciuriware
sciuriware

This doesn't make things clear.

Is "MyDateToConvertFormat" a Date or not.
There is no such thing in the world as an ISO standard object.
You need an ISO standard String.
That should be produced by the formatter.
Try this example:

Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String output = formatter.format(d);
System.out.println(output);

What is the output?

;JOOP!
Avatar of TyBay
TyBay

ASKER

MyDateToConvertFormat is a date object that is returned from our vendors api.  I am successful in formatting and then converting to return a string when creating a new date object.  Unfortunately, I am not successful in manipulating the date object returned from the vendor API
Avatar of sciuriware
sciuriware

Then what date string can you get out of that weird object?

Tell me and we'll convert it to what you want.

;JOOP!
Avatar of TyBay
TyBay

ASKER

The date string returned from this wonderful date object is always in below format:
string example=Mon Jan 22 04:47:28 PST 2007.
pattern=EEE MMM dd HH:mm:ss z yyyy

I need a date string like below:
desired string example=2007-02-24T08:09:51.
pattern=yyyy-MM-dd'T'HH:mm:ss

Many Thanks
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>
The date string returned from this wonderful date object is always in below format:
string example=Mon Jan 22 04:47:28 PST 2007.
>>

TyBay, the way the Date prints out by default is of no importance whatever. If you want it to print out in the ISO format then use the DateFormat that has already been given to you
Avatar of sciuriware
sciuriware

Let him try my code' I'm curious for new facts.

;JOOP!
Avatar of TyBay
TyBay

ASKER

It seems the below line is expecting a string and not java.util.date object:
d = importFormat.parse(input);

Its strange - I am using a contect manager in the API and when I use +MyDateObject+, the string that is presented is correct, but as I said the format is not.

I tried to convert the date object to a string with .tostring() to try the previous suggestion and then use that string instead of +MyDateObject+, and am getting the same run time error.  I am beginning to think this is just not feasible in the API...
 
Avatar of TyBay
TyBay

ASKER

If it is an immutable Date object that is returned.  Is there any way around this?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>I tried to convert the date object to a string with .tostring()

We've already said that the *default* toString (how it prints itself) is not important- you can choose the format you want be using the DateFormat
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>the *default* toString (how it prints itself) is not important-

(and does *not* print in an ISO format). You choose whatever format you want.

Please say again

1.What you're starting with - Date or String
2. What you want to do with it
Avatar of sciuriware
sciuriware

TyBay, you said:

 The Date object is in the below format when it is returned:
Mon Jan 22 04:47:28 PST 2007.

You must use that string every time.
My solution was based on your messages.

;JOOP!
Avatar of sciuriware
sciuriware

:)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo