基于GMT/UTC而非用户的Android倒计时';s时区
这里有个小问题。我有一个安卓倒计时器,工作正常,但问题是倒计时器在用户/时区之间会有所不同。我的意思是,对于每个时区,我的应用程序的用户都会看到不同的倒计时,相对于他们的时区,倒计时可能会提前或延迟结束。我不想要这个,我想要它,所以对于本次电影发行,所有倒计时都会同时结束 我的代码: 注意:为了简洁起见,省略了一些代码,请查看下面的注释以了解代码的一些解释基于GMT/UTC而非用户的Android倒计时';s时区,android,datetime,calendar,timezone,Android,Datetime,Calendar,Timezone,这里有个小问题。我有一个安卓倒计时器,工作正常,但问题是倒计时器在用户/时区之间会有所不同。我的意思是,对于每个时区,我的应用程序的用户都会看到不同的倒计时,相对于他们的时区,倒计时可能会提前或延迟结束。我不想要这个,我想要它,所以对于本次电影发行,所有倒计时都会同时结束 我的代码: 注意:为了简洁起见,省略了一些代码,请查看下面的注释以了解代码的一些解释 // the current date and time Calendar today = Calendar
// the current date and time
Calendar today = Calendar.getInstance();
// release.date is in milliseconds at 12am and in GMT, example: GMT: Friday, September 29, 2017 12:00:00 AM
long currentTimeMillis = today.getTimeInMillis();
long expiryTime = releaseDate.date - currentTimeMillis;
holder.mTextCountdown.setText("");
if (holder.timer != null) {
// Cancel if not null to stop flickering
holder.timer.cancel();
}
holder.timer = new CountDownTimer(expiryTime, 500) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000; // reminder: 1 sec = 1000 millis
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
String dayFormat = "days";
String hoursFormat = "hours";
if (days == 1) {
dayFormat = "day";
}
if (hours == 1) {
hoursFormat = "hour";
}
String time = days + " " + dayFormat + " : " + hours % 24 + " " + hoursFormat +" : " + minutes % 60 + " : " + seconds % 60;
holder.mTextCountdown.setText(time);
}
// Finished: counted down to 0
public void onFinish() {
holder.mTextCountdown.setText("Now out!");
}
}.start();
我如何以GMT/UTC为单位倒计时,每个用户的计时器都会在同一时间结束
谢谢你我建议你做以下两件事:
我建议你做以下两件事:
使用现代java.time类,而不是麻烦的旧
日历
/日期
类。对于Android,请参见下面最后的项目符号
假设你的电影于9月23日洛杉矶时间下午5点上映
LocalDate ld = LocalDate.of( 2017 , 9 , 23 ) ;
LocalTime lt = LocalTime.of( 17 , 0 ) ;
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
将其转换为UTC,即即时对象
Instant instant = zdt.toInstant() ; // Convert from a time zone to UTC. Same point on the timeline.
计算到那时的时间,作为一个例子
提取毫秒数
long millis = d.toMillis() ;
也许您想向魁北克的用户显示电影发行日期和时间
ZonedId zUser = ZoneId.of( "America/Quebec" ) ;
ZonedDateTime zdtUser = instant.atZone( zUser ) ; // Adjust into user’s time zone. Some point on the timeline.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = zdtUser.format( f ) ;
关于java.time
该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&
该项目现已启动,建议迁移到类
要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是
从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 该项目专门为Android采用了ThreeTen Backport(如上所述)
- 看
使用现代java.time类,而不是麻烦的旧日历
/日期
类。对于Android,请参见下面最后的项目符号
假设你的电影于9月23日洛杉矶时间下午5点上映
LocalDate ld = LocalDate.of( 2017 , 9 , 23 ) ;
LocalTime lt = LocalTime.of( 17 , 0 ) ;
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
将其转换为UTC,即即时对象
Instant instant = zdt.toInstant() ; // Convert from a time zone to UTC. Same point on the timeline.
计算到那时的时间,作为一个例子
提取毫秒数
long millis = d.toMillis() ;
也许您想向魁北克的用户显示电影发行日期和时间
ZonedId zUser = ZoneId.of( "America/Quebec" ) ;
ZonedDateTime zdtUser = instant.atZone( zUser ) ; // Adjust into user’s time zone. Some point on the timeline.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = zdtUser.format( f ) ;
关于java.time
该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&
该项目现已启动,建议迁移到类
要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是
从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 该项目专门为Android采用了ThreeTen Backport(如上所述)
- 看
所有时间都已使用GMT,很抱歉造成混淆,实际上对于UI输出,我会使用GMT转换,而不是用户的时区,因为这样它就不会打印正确的发布日期。所有时间都已使用GMT,很抱歉造成混淆,对于UI输出,我会使用GMT转换,而不是用户的时区,因为那样它就不会打印出正确的发布日期了。非常感谢。非常感谢