31 votes
Accepted

How to set a Date in apex test class?

You can do this using Apex' Date class: acc.effectiveDate__c = Date.newInstance(2016, 12, 9); For more information check out the documentation: Apex Developer Guide - Date Class
Markus Dang - formerly Slabina's user avatar
28 votes
Accepted

System vs Date/DateTime class

They give you the same data. Their descriptions are almost verbatim as well: System.now() Returns the current date and time in the GMT time zone. Datetime.now() Returns the current Datetime based on ...
Adrian Larson's user avatar
  • 149k
27 votes
Accepted

JSON: Cannot deserialize instance of date from VALUE_STRING

Three problems here: (a) Date receiving DateTime value. The value should be "YYYY-MM-DD" only. (b) Decimal receiving String value. The value should not be in quotes. (c) You must not have a ...
sfdcfox's user avatar
  • 477k
23 votes

System.TypeException: Invalid date/time: 2019-01-03T00:26:01.711-02:00

To parse DateTimes in this ISO format, you need to use JSON deserialization. String data = '2019-01-03T00:26:01.711-02:00'; Datetime dt = (DateTime)JSON.deserialize('"' + data + '"', DateTime.class); ...
David Reed's user avatar
  • 91.7k
17 votes
Accepted

DateTime.format() unexpected behavior

Actually... the datetime format is right. The capital Y merge field is the WEEK YEAR - which from the 27th December 2015 is IN 2016! The lower case y merge field is the CALENDAR YEAR - which will be ...
Simon Lawrence's user avatar
17 votes
Accepted

DateTime.day() returning inconsistent value when created from system.today()

The problem was with initializing the DateTime variable by casting from a Date. As the Date had no Timezone information, the resulting DateTime was created at UTC+0. For Pacific Standard Time at the ...
Daniel Ballinger's user avatar
17 votes
Accepted

How To Format Datetime like 2017-01-10T14:53:55.000+0000?

Using the example given in the DateTime class and calling the formatGMT(string) method with a formatting string found in the Java SimpleDateFormat examples: DateTime myDateTime = DateTime.newInstance(...
Mark Pond's user avatar
  • 22.8k
16 votes

How can I get my correct current local time?

This code will get you the local time for the logged in user, by leveraging the timezone offset to subtract/add from GMT. It's even smart enough to account for Daylight Saving. TimeZone tz = UserInfo....
Sebastian Kessel's user avatar
15 votes

Is there a default TimeZone in which Date fields are stored in Salesforce?

I know this question is very old, but it comes up as one of the top results on google for Salesforce date timezone questions. DateTime fields store the time information in UTC and display the ...
Egor's user avatar
  • 315
15 votes
Accepted

How to convert GMT to locale dateTime?

Your question is not very clear to me, but my two possible interpretations are you want to format the current Datetime according to your locale, or you want to adjust the System.now() result to ...
Adrian Larson's user avatar
  • 149k
14 votes

System vs Date/DateTime class

Historically, only the System methods were originally available (System.now and System.today). The newer methods were added later, presumably because people felt it was logical that you'd find the ...
sfdcfox's user avatar
  • 477k
14 votes
Accepted

Parse date time string to Apex DateTime

JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class); Note the extra set of double quotes - they allow the JSON parser to interpret that value.
David Reed's user avatar
  • 91.7k
13 votes
Accepted

Convert Datetime to string in GMT format?

Use DateTime's formatGMT to format the time according to your specs in GMT. String formatedDt = dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
sfdcfox's user avatar
  • 477k
11 votes
Accepted

Process Builder - Add Minutes,Hours to Event End Date

Use the Formula like Now() + (60/24/60) Give your required minutes in first 60. If you want for 10 minutes to be added, Give like Now() + (10/24/60) Then you will get the 60 minutes added to the ...
Subhash's user avatar
  • 1,706
11 votes
Accepted

What's the correct format to set a default value to a DateTime aura:attribute?

Very interesting question. I did some research on it and found this gitHub repository, someone has tried to play with default values of all kinds of basic types. Scene 1: Simply putting the below ...
Santanu Halder's user avatar
10 votes
Accepted

DateTime.Format() Documentation example causes exception

Attribution Use: system.debug(Datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'')); The esteemed Mr Ballinger also recommends the following, but it is wrapped in double quotes and slower, ...
Adrian Larson's user avatar
  • 149k
10 votes
Accepted

Illegal assignment from Datetime to Date

You can get the Date value from a Datetime instance by calling the date() method: obj2.Date__c = obj1.CreatedDate.date(); If you're calling this method on a Datetime instance where you are not sure ...
Adrian Larson's user avatar
  • 149k
9 votes
Accepted

Deserializing a string from JSON to DateTime in Apex yields "Invalid format" error

You must use an ISO 8601 date, specifically something like: 2016-10-30T15:27:02.000Z Which is October 30th, 2016, at 3:27 pm (+2 seconds) in Greenwich Mean Time. To parse dates in the user's locale,...
sfdcfox's user avatar
  • 477k
9 votes

How to change date format depends on user locale?

There is a Date method for that: format() Returns the Date as a string using the locale of the context user Here are what those formats would look like: LocaleSidKey toLabel(LocaleSidKey) ...
Adrian Larson's user avatar
  • 149k
9 votes

What are the minimum and maximum valid dates in Salesforce?

Initially I have found an answer here, but the link provided in that answer, was broken. Finally found a corresponding valid link in the documentation Minimum and Maximum Dates Only dates ...
Patlatus's user avatar
  • 15.8k
8 votes
Accepted

Date is Subtracting 1 day after converting to other format

DateTime.format() ... Converts the date to the local time zone and returns the converted date as a formatted string using the locale of the context user Since you're converting a Date value (not a ...
David Reed's user avatar
  • 91.7k
7 votes

How Can I Tell the Day of the Week of a Date?

For reference, an Apex code version of the modular division approach is: public static Integer dayOfWeekNumber(Date aDate) { return Math.mod(Date.newInstance(1900, 1, 7).daysBetween(aDate),7); } ...
Rick Coates's user avatar
7 votes
Accepted

24 hours format from date/time field

You'll want to change your formula to look like: "Date/time field".format('kk:mm:ss'); See the Java Simple Date Time Format Page for more.
crmprogdev's user avatar
  • 40.8k
7 votes

Formating Date time field in excell to import via data loader

You want to format it as: yyyy-mm-ddThh:mm:ssZ Where you can just make hh:mm:ss be 00:00:00. e.g. 2016-01-18T00:00:00Z
Nick Cook's user avatar
  • 6,354
7 votes

How to change the `LastModifiedDate` of a record in a test class?

There are 2 ways in which you can set system protected fields for test code coverage: a) Using Test.loadData() b) Using JSON.deserialize() Using Test.loadData. In this option you will need to ...
C. Praveenkumar's user avatar
7 votes
Accepted

Datetime to Date to String

If you want to specify a particular format (not just the default for your locale), leave it as Datetime. That type has a format method signature which accepts a String. You can supply any supported ...
Adrian Larson's user avatar
  • 149k
7 votes

How to set a Date in apex test class?

Another option is using system.today() (or system.now() if it's datetime field): acc.effectiveDate__c = System.today() + 5;
Liron C's user avatar
  • 1,764
7 votes

How can I get my correct current local time?

DateTime values are represented by an epoch time in GMT, and converted to the appropriate date/time when requested via the appropriate methods. System.debug, coincidentally, always displays GMT. Try ...
sfdcfox's user avatar
  • 477k
7 votes

Date in RFC2822 to Apex DateTime?

I'm not aware of any standard way to parse this, but here's a quick method to do it: Map<String, String> months = new Map<String, String> { 'Jan' => '01', 'Feb' => '02', ...
IllusiveBrian's user avatar
7 votes

Test.setCreatedDate not working in SOQL Query with condition date set in future

It seems that you can set a CreatedDate on a record in the future, but when querying for that record you cannot use a date in the future as condition Although it is not likely that you will want to ...
Wim's user avatar
  • 564

Only top scored, non community-wiki answers of a minimum length are eligible