android - 如何在当前时间中添加分钟数并从当前时间中减去它以运行计时器?

标签 android firebase

我正在尝试将分钟数添加到 android 中的当前时间。

我想将简单日期格式用作("EEE, d MMM yyyy HH:mm:ss Z")

首先我想解释一下我的概念是什么。

  1. 有一个 Recyclerview 列表,在每一行中,用户选择一个时间。假设我选择 50 分钟。这 50 分钟被添加到当前时间并存储在数据库中。

我用过代码...

 String t = time.getText().toString();
 int ti = Integer.parseInt(t);
 @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
 Calendar now = Calendar.getInstance();
 String a = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
 now.add(Calendar.MINUTE,ti);
 a = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
 adminList = mFirestore.collection("AdminList");
               Map<String,Object> k = new HashMap<>();
               k.put("order",order);
               k.put("status","Pending");
               k.put("hotel_name",hotel);
               k.put("user_id",userid);
               k.put("time",a);
               adminList.document(order).update(k);
  1. 用户从数据库中获取这个时间,然后从当前每秒时间中减去它,以便列表显示为倒数计时器。

代码是。

 void updateTimeRemaining(String text) {
        if (o.getOrder().equalsIgnoreCase("order1")) {
        try {
            TimeZone.setDefault(TimeZone.getDefault());
            String stop = ol.getTime();
            @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
            Calendar end_date = Calendar.getInstance();
            end_date.setTime(format.parse(stop));

            final Calendar today = Calendar.getInstance();
            timeDiff = end_date.getTimeInMillis() - today.getTimeInMillis();
            if (timeDiff > 0) {
                long seconds = timeDiff / 1000 % 60;
                long minutes = timeDiff / (60 * 1000) % 60;
                long hours = timeDiff / (60 * 60 * 1000) % 24;
                //long days = (int) timeDiff / (24 * 60 * 60 * 1000);
                //long days = TimeUnit.MILLISECONDS.toDays(timeDiff);


                String left = "";
                //if (days > 0)
                //   left += days + " " + /*context.getString(R.string.txt_day) +*/ ":";
                if (hours > 0)
                    left += hours + " " +/* context.getString(R.string.txt_hour) + */":";
                if (minutes > 0)
                    left += minutes + " " +/* context.getString(R.string.txt_minute) + */":";

                left += seconds + " " /*+ context.getString(R.string.txt_second)*/;

                String finalLeft = left;
                text = finalLeft;
                time.setText("00:" + finalLeft);

            } else {
                time.setText("Finished");
            }
//                    }


        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //   }
    }

所以我的最后一个问题是,

如何将当前时间加 50 分钟,格式为 "EEE, d MMM yyyy HH:mm:ss Z"

最佳答案

不要让自己头疼,使用像 Jodatime 或 ThreeTenABP 这样的库。

您的代码可以像这样简单:

DateTimeFormatter dateFormatter = DateTimeFormat
            .forPattern("EEE, d MMM yyyy HH:mm:ss Z");

LocalTime now = new LocalTime();
LocalTime then = now.plusMinutes(50);

Log.d(TAG, dateFormatter.print(then));

在 Java 8 下管理日期和时间是一个不可读的不可靠的困惑;您现在可能已经意识到了。

关于android - 如何在当前时间中添加分钟数并从当前时间中减去它以运行计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184286/

相关文章:

android - 如何在 NetBeans 的 Android 项目中使用外部 jar?

java - Android 应用请求 Firebase 自定义身份验证

android - Firebase Android : onDataChange() event always executed in Main UI Thread?

Android - PendingIntent.getActivity 中的上下文参数

android - 从异步回调更新 Ionic View

ios - 在 iOS 中集成 FCM 以进行推送通知时,是否使用了 Device Token?

javascript - 如何检查用户参数以匹配Firebase中的值?

java - 下载到内存后如何访问文件,Firebase?

android - 在运行 Android 11 的设备上,targetSdkVersion 22 底部出现黑色空间

java - NavigationDrawer 与 fragment 或 Activity ?另外,请教我正确的 fragment 管理