SimpleDateFormat always returns 1970.01.17 with wrong timezone

一曲冷凌霜 提交于 2019-12-05 10:18:41
M S Parmar

The Date object parameter accepts the time as long in milliseconds, not seconds. You need to multiply it by 1000. and make sure that you supply it as long.

Date dateObj = new Date(1442915733L * 1000);
System.out.println(dateObj);

Decided to post this as an answer, since it's different from Mitesh's solution.

I dropped the getTimeNow() function and instead I simply created a new date and used that:

void setup ()
{      
  SimpleDateFormat format = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss z");
  format.setTimeZone (TimeZone.getDefault());

  Date timestamp = new Date ();
  println(format.format(timestamp));
}

The reason is two fold. I had another issue with the code, after applying Matesh's answer, where the TimeZone would disregard the DST settings, so the hour in the timestamp was wrong. Furthermore, this solution removed the need for several lines of code, which is always helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!