0

How can I convert in Java a POSIXct date to a formatted date like "y-M-d". I've been looking around but I didn't found anything.

I read from a file these lines:

1398691820,517.0400,517.0400,517.0400,517.0400,56200
1398691870,517.1950,517.1950,516.8850,517.0350,2700
1398691920,516.2500,517.2050,516.2500,517.2050,5800
1398691980,515.6000,516.4408,515.6000,516.2800,7700
1398692044,515.4800,515.9700,515.4800,515.9200,5300

The first number of each line is a POSIXct date and I need to convert it to y-M-d hour:min.

I also have in the file the offset:

timezone:EDT
gmtoffset:-14400

Any help is very apreciated.

Thanks a lot.

Edit:
This the way to convert the date in R:

as.POSIXct(1398691820-1400*60, origin = '1970-01-01', tz='EST')
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
fpinero
  • 5
  • 2
  • Well, that sounds distinctly feasible - multiply your value by 1000 to get milliseconds since the Unix epoch, create a `Date` from that, and format with `SimpleDateFormat`. The time zone part will be "interesting" - do you have an offset per entry, or for the whole file? What have you tried so far? There's a *lot* of information about converting from a Unix timestamp to a `java.util.Date`, and a lot of information about formatting dates... – Jon Skeet Apr 29 '14 at 10:06
  • Hi, offset is for the whole file. I'm going to try what you propose. Thank You – fpinero Apr 29 '14 at 10:10
  • it doesn't work. This posix date 1357828200 converted correctly with as.POSIXct(1357828200, origin = '1970-01-01', tz='EST') is "2013-01-10 14:30:00 EST". In Java I try String sfechaPosix = df.format(1398691820 * 1000) and returns 1969-12-15 01:21 i'll keep trying. – fpinero Apr 29 '14 at 10:52
  • 1
    You should post your *complete* code, not just bits and pieces. Note that you should perform the multiplication using `long` arithmetic, not `int` arithemtic, otherwise you'll get overflow. (I suspect that's the issue you're seeing.) Just use `1000L` for example. – Jon Skeet Apr 29 '14 at 10:57
  • You are right. Works perfectly. I forgot to force 'long'. My fault. Thank You very much. – fpinero Apr 29 '14 at 11:16
  • possible duplicate of [Java: Date from unix timestamp](http://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp) – Basil Bourque Jun 20 '14 at 02:16

0 Answers0