1

I am trying to add a number of minutes to the current Time in android.

I want to use the Simpledateformat as ("EEE, d MMM yyyy HH:mm:ss Z")

Firstly I want to explain what my concept is.

  1. There is a Recyclerview List where on each Row, A user selects a time. suppose I select 50 Minutes. this 50 minutes is added to the current time and stored in the database.

I have used the code...

 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. The User Gets This Time From the Database, and subtract it from the current Time per second so that the list is shown with a Countdown Timer.

code for this is.

 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();
        }
        //   }
    }

So My Final Problem is,

How can I add 50 minutes to the Current Time and with the format "EEE, d MMM yyyy HH:mm:ss Z"

Jignesh Mistry
  • 2,221
  • 6
  • 25
  • 50
rituparna
  • 91
  • 1
  • 12
  • 1
    Possible duplicate of [How to add minutes to my Date](https://stackoverflow.com/questions/9043981/how-to-add-minutes-to-my-date) – Ole V.V. Jul 05 '18 at 09:48
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 05 '18 at 10:23
  • 1
    Yes I got it. its easy and simple. thanks again – rituparna Jul 05 '18 at 12:37
  • Part of this Question is a duplicate of [*Get current time and date on Android*](https://stackoverflow.com/q/5369682/642706). And a duplicate of many Questions about [adding minutes in Java](https://stackoverflow.com/search?q=java+add+minutes). Please search Stack Overflow thoroughly before posting. – Basil Bourque Jul 06 '18 at 00:37

1 Answers1

2

Save yourself some headache and use a library like Jodatime or ThreeTenABP.

Your code can be as simple as:

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));

Managing date and time below Java 8 is an unreadable unreliable mess; you probably already realised it by now.

terencey
  • 3,282
  • 4
  • 32
  • 40