代码之家  ›  专栏  ›  技术社区  ›  rituparna

如何在当前时间中添加分钟并从当前时间中减去分钟来运行计时器?

  •  1
  • rituparna  · 技术社区  · 8 年前

    我正在尝试在android中为当前时间添加一些分钟。

    我想使用simpleDateFormat作为 (“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”

    1 回复  |  直到 8 年前
        1
  •  2
  •   terencey    8 年前

    省省点头痛,使用像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下管理日期和时间是一个不可读取的不可靠的混乱;您可能已经意识到了这一点。

    推荐文章