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
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 ...
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 ...
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);
...
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 ...
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 ...
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(...
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....
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 ...
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 ...
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 ...
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.
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\'');
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 ...
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 ...
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, ...
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 ...
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,...
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) ...
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 ...
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 ...
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);
}
...
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.
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
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 ...
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 ...
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;
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 ...
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',
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
datetime × 732apex × 310
date × 165
timezone × 87
formula × 72
visualforce × 52
soql × 52
marketing-cloud × 50
formula-field × 44
time × 36
formatting × 25
ampscript × 23
json × 22
lightning × 21
lightning-aura-components × 20
lightning-web-components × 20
trigger × 19
format × 18
javascript × 17
query × 15
process-builder × 14
string × 14
visual-workflow × 13
businesshours × 13
unit-test × 12