SZH*_*SZH 30 java android date milliseconds
我正在创建一个我想在午夜每晚更新的Android小部件.我正在使用一个AlarmManager,我需要知道从当前时间到午夜剩下多少毫秒.这是我的代码:
AlarmManager mAlarmManager = (AlarmManager)context.getSystemService(android.content.Context.ALARM_SERVICE);
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, millisecondsUntilMidnight, mSrvcPendingingIntent);
Run Code Online (Sandbox Code Playgroud)
如何计算到午夜剩下多少毫秒?
先感谢您.
Den*_*ret 51
使用日历来计算它:
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long howMany = (c.getTimeInMillis()-System.currentTimeMillis());
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 14
Duration.between( now , tomorrowStart )
.toMillis()
Run Code Online (Sandbox Code Playgroud)
Java 8及更高版本内置了java.time框架.这些新类取代了与Java捆绑在一起的旧日期时间类(java.util.Date/.Calendar).也取代了Joda-Time,由同一个人开发.大部分java.time功能都被反向移植到Java 6和7,并进一步适应Android(见下文).
时区对于确定日期至关重要.对于任何给定的时刻,日期在全球范围内因地区而异.例如,法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是"昨天" .
指定适当的时区名称,格式continent/region,如America/Montreal,Africa/Casablanca或Pacific/Auckland.切勿使用3-4字母缩写,例如EST或IST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!).
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
Run Code Online (Sandbox Code Playgroud)
我们希望得到第二天的第一时刻,但不包括第二时刻的毫秒数.
我们必须通过LocalDate课程才能获得一天的第一时间.所以在这里,我们开始与ZonedDateTime获得LocalDate,之后再弄ZonedDateTime.关键是呼吁atStartOfDay的LocalDate.
LocalDate tomorrow = now.toLocalDate().plusDays(1);
ZonedDateTime tomorrowStart = tomorrow.atStartOfDay( z );
Run Code Online (Sandbox Code Playgroud)
请注意,我们不会在00:00:00对时间进行硬编码.由于夏令时(DST)等异常,白天可能会在01:00:00等其他时间开始.让java.time确定第一时刻.
现在我们可以计算经过的时间.在java.time中我们使用Duration该类.java.time框架具有更精细的纳秒分辨率,而不是java.util.Date和Joda-Time使用的更粗糙的毫秒.但正如问题所要求的那样,Duration包括一种方便的getMillis方法.
Duration duration = Duration.between( now , tomorrowStart );
long millisecondsUntilTomorrow = duration.toMillis();
Run Code Online (Sandbox Code Playgroud)
请参阅IdeOne.com上的此代码.
now.toString():2017-05-02T12:13:59.379-04:00 [美国/蒙特利尔]
tomorrowStart.toString():2017-05-03T00:00-04:00 [美国/蒙特利尔]
millisecondsUntilTomorrow:42360621
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
UPDATE:该乔达时间的项目现在处于维护模式,与团队的建议迁移java.time类.为了历史原因,我保留此部分的完整性,但建议使用java.time和ThreeTenABP,如上所述.
在Android中,你应该使用Joda-Time库而不是臭名昭着的麻烦的Java.util.Date/.Calendar类.
Joda-Time提供一个毫秒的命令.但我们并不需要这里.
相反,我们只需要一个Duration对象来表示直到第二天的第一个时刻的时间跨度.
时区对于确定"明天"何时开始至关重要.通常更好地指定,而不是隐含地依赖于JVM的当前默认时区,该时区可以随时改变.或者,如果您真的想要JVM的默认设置,请通过调用来明确请求,DateTimeZone.getDefault以使您的代码自我记录.
可能是一个双线.
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
long milliSecondsUntilTomorrow = new Duration( now , now.plusDays( 1 ).withTimeAtStartOfDay() ).getMillis();
Run Code Online (Sandbox Code Playgroud)
让我们分开吧.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); // Or, DateTimeZone.getDefault()
DateTime now = DateTime.now( zone );
DateTime tomorrow = now.plusDays( 1 ).withTimeAtStartOfDay(); // FYI the day does not *always* start at 00:00:00.0 time.
Duration untilTomorrow = new Duration( now , tomorrow );
long millisecondsUntilTomorrow = untilTomorrow.getMillis();
Run Code Online (Sandbox Code Playgroud)
转储到控制台.
System.out.println( "From now : " + now + " until tomorrow : " + tomorrow + " is " + millisecondsUntilTomorrow + " ms." );
Run Code Online (Sandbox Code Playgroud)
跑步时
从即日起:2015-09-20T19:45:43.432-04:00至明天:2015-09-21T00:00:00.000-04:00为15256568 ms.
| 归档时间: |
|
| 查看次数: |
22364 次 |
| 最近记录: |