I found the question about converting epoch time to normal time, but I'm having two problems:
- When I run DateTime.newInstance(1391529600 * 1000) I am getting the wrong answer
- I want to convert normal time to epoch time
1: I have been typing the following into Execute Anonymous:
system.debug(DateTime.newInstance(1391529600 * 1000));
By my calculations (and some websites I found) that should give me 4 Feb, 2014, 16:00:00 GMT.
Apex gives me
1969-12-31 12:56:36
Clearly, this is incorrect (and it's even before 1/1/1970). Can anyone replicate my situation?
2: I also want to change the current datetime (GMT) to epoch time, returning the number of seconds or milliseconds since 1/1/1970. I could break things into parts, but that seems an awful lot of script statements to do something that should be fairly easy, I'd hope.
Thanks for the help!
Best Answer
I can replicate it, and by removing the
* 1000
and appending 3 zeros, I got "Invalid Integer". So what I believe is that1391529600 * 1000
is being evaluated as an integer type and so overflowing.You can create the Long value using the
L
notation like so:To get the epoch from a
datetime
variable you can just call thegetTime()
instance method:produces: