fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.Date;
  8. import java.sql.Timestamp;
  9.  
  10. import java.util.Calendar;
  11. import java.text.SimpleDateFormat;
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15.  
  16.  
  17. public static void main (String[] args) throws java.lang.Exception {
  18.  
  19.  
  20. Timestamp stamp = new Timestamp(1471212000); //puede usar un long directamete yo lo uso como contenedor
  21. Date date_f = new Date(stamp.getTime() * 1000L);
  22.  
  23. //System.out.println(date_f.toString());
  24. System.out.println(date_f);
  25.  
  26. Date date_test = new Date(stamp.getTime());
  27.  
  28. //esto imprime mas o menos lo mismo que uste publica
  29. //fijese que stamp.getTime() no esta *1000L puede hacer la comprovacion
  30. //en este link:
  31. //http://w...content-available-to-author-only...r.com/timezones?q=1471212000&tz=America%2FNew_York
  32.  
  33. System.out.println(date_test);
  34. System.out.println("///");
  35.  
  36. //puede ver que al hacer Date(stamp.getTime() * 1000L); el resultado es similar
  37.  
  38. //fijese que uno es 1970 y otro 2016 eso es porque uno tiene aplicado *1000L
  39.  
  40. //he busacado este respuesta igual entiende mejor lo que le digo:
  41. //http://stackoverflow.com/questions/17432735/convert-unix-time-stamp-to-date-in-java
  42.  
  43. //Saludos!
  44.  
  45. String miJSONTimeStamp = "1471212000";
  46. Long miTimeStamp = Long.parseLong(miJSONTimeStamp);
  47.  
  48. //Define formato de salida deseado.
  49. String formatoDeseado = "yyyy-MM-dd hh:mm:ss";
  50. SimpleDateFormat formatter = new SimpleDateFormat(formatoDeseado);
  51.  
  52. //Crea objeto para convertir millisegundos a fecha.
  53. Calendar calendar = Calendar.getInstance();
  54. calendar.setTimeInMillis(miTimeStamp);
  55.  
  56. //aplica formato
  57. String fechaconFormat = formatter.format(calendar.getTime());
  58.  
  59. System.out.println("Test sin *1000L");
  60. System.out.println(fechaconFormat);
  61.  
  62. ///////////////////////////////////
  63.  
  64.  
  65. Calendar calendar1 = Calendar.getInstance();
  66. calendar1.setTimeInMillis(miTimeStamp * 1000L); // <-- Aplicamos *1000L
  67.  
  68. //aplica formato
  69. String fechaconFormat1 = formatter.format(calendar1.getTime());
  70.  
  71. System.out.println("Test con *1000L");
  72. System.out.println(fechaconFormat1);
  73. }
  74. }
Success #stdin #stdout 0.14s 321344KB
stdin
Standard input is empty
stdout
Sun Aug 14 22:00:00 GMT 2016
Sun Jan 18 00:40:12 GMT 1970
///
Test sin *1000L
1970-01-18 12:40:12
Test con *1000L
2016-08-14 10:00:00