function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MintMint 

Retrieving Date field from query results (Java, salesforce)

Hi,

 

I am very new to java development for SalesForce integration.

 

I got SObject as a result of query from the SalesForce. Now, I need to retrieve TimeZone information and the timestamp from the SObject's "CreatedDate" column. 

 

Can anyone help me how to retrieve this information? I see the following classes related to this:

 

GetServerTimestamp.java

GetServerTimestampResponse.java

GetServerTimestampResult.java

 

But, don't know how to use them.

 

Any help is greately apprciated.

 

Thanks.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MintMint
Thank you so much, problem solved!!

All Answers

SuperfellSuperfell
If you're using the enterprise API, just cast the SObject to its concrete type, and call the getter for CreatedDate, it'll be mapped to a Date or Calendar for you (depending on which toolkit you're using), see the examples in the API docs.
MintMint

Sorry, I didn't mention that I am using partner wsdl. The examples do not have anything for partner wsdl.

 

Thanks.

mpiercempierce

Looks like CreatedDate is in ISO 8601 format:

2008-07-29T18:35:18.000Z

 

You can use Joda Time to parse the ISO 8601 string into a DateTime object. Once you have the DateTime object, you can easily get the time zone information. See the Joda Time documentation for details.

 

You can also use the standard library classes (Calendar, etc) but Joda Time has a much better API, is thread safe, etc.

MintMint
Thank you so much, problem solved!!
This was selected as the best answer
Rick.BanisterRick.Banister
The time zone for API calls is always GMT, no matter what the user setting is.
mpiercempierce
Technically it's 'zulu time', which is UTC not GMT. However, UTC and GMT are *almost* identical and for most practical purposes are interchangeable.
MintMint

Thanks so much all of you, for sharing this valuable information. This is what I am doing:

 

String utcTime = (String) sObjAttributes.get("CreatedDate");

 

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss");TimeZone tz = TimeZone.getTimeZone("GMT");

dateFormat.setTimeZone(tz);

Date utcDate = dateFormat.parse(utcTime);

 

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(utcDate);

 

This converts the utcTime from the SalesForce to my local time.

mpiercempierce

Try just this:

 

String utcTime = (String) sObjAttributes.get("CreatedDate");

DateTime dt = new DateTime(utcTime);

 

Since the string is already in ISO8601 format, you shouldn't need to do all that work to parse it. You now have a DateTime in UTC time. To see what the time would be in another time zone:

 

DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));

 

MintMint

Appreciate your reply. This is definitely a better way of doing it.

 

Thanks.

gurditta.garg@gmail.comgurditta.garg@gmail.com

Hello,

 

Please share your solution if you have found it correctly..

 

thanks