Android 如何获取日期值自1970年以来经过的秒数?
我需要一个android应用程序,其中我需要将当前日期转换为自1970年以来经过的秒数 我现在正在做的就是这个Android 如何获取日期值自1970年以来经过的秒数?,android,Android,我需要一个android应用程序,其中我需要将当前日期转换为自1970年以来经过的秒数 我现在正在做的就是这个 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(currentDate.get(Calendar.YEAR), month, currentDate.get(Calendar.DAY_OF_MONTH), 0, 0, 0); long timesince1970 = cal.getTi
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.set(currentDate.get(Calendar.YEAR), month, currentDate.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
long timesince1970 = cal.getTime().getTime();
另一个问题是我的应用程序需要在德国运行。因此,需要将时间转换为其时区,然后将其转换为自1970年以来的秒数
上面给出的结果不正确。给出了自Unix纪元(1970年1月1日00:00:00 UTC)以来的毫秒数
将其除以1000得到秒数。因为它来自
System
docurrentTimeMillis()
如果用户更改了日期,它会变化吗?@CliffBurton:是的,它会变化。Android手机通常设置为自动同步时间,但返回的日期可能是错误的。如果你需要准确的时间,你通常需要做一些在线检查。(注意:由于返回的时间是UTC,因此不受DST或时区的影响,除非出现错误)您应该解释代码回答问题的原因。
//long currentTimeMillis ()-Returns the current time in milliseconds.
long millis = System.currentTimeMillis();
//Divide millis by 1000 to get the number of seconds.
long seconds = millis / 1000;