我想在我的应用程序中实现一个计时器,它会显示新更新的发布时间。 重要的是,使用我的应用程序的每个人都会看到相同的计时器以及相同的剩余时间。
有人知道如何存档吗?
更新:
到目前为止我尝试过:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "11.10.2019, 15:23:00";
Date endDate;
try {
endDate = formatter.parse(endTime);
timer = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
startTime = System.currentTimeMillis();
diff = timer - startTime;
new CountDownTimer(timer, 1000) {
@Override
public void onTick(long millisUntilFinished) {
startTime=startTime-1;
long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;
String hms = (String.format("%d", serverUptimeSeconds / 86400)+"d "+String.format("%d", (serverUptimeSeconds % 86400) / 3600)+"h "+ String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60))+"m " + String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60)+"s ";
counter.setTitle(hms);
}
它根据我写入的日期和时间显示剩余时间
String endTime = "11.10.2019, 15:23:00";
以及用户时区。
我怎样才能让每个人看到的剩余时间不是基于他们的时区,而是基于我的时区:GMT+2?
最佳答案
您只能获取一个时区的时间。所有用户都会看到相同的时间结果,与用户的时区无关
TimeZone tz = TimeZone.getTimeZone("GMT+02:00");
Calendar c = Calendar.getInstance(tz);
String time = String.format("%02d" , c.get(Calendar.HOUR_OF_DAY))+":"+
String.format("%02d" , c.get(Calendar.MINUTE))+":"+
String.format("%02d" , c.get(Calendar.SECOND))+":"+
String.format("%03d" , c.get(Calendar.MILLISECOND));
关于java - 我如何编写一个每个人都看到的公共(public)计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334953/