简体   繁体   中英

How can I add an hour on to the time set but the SimpleDateFormat?

I have a start button with the current time displaying and I want to be able to have a button with a stop time that is an hour later than the current time. How can I go about doing this?

Here is my code for the button displaying the current time

Button stopButton = (Button) this
            .findViewById(R.id.StartTrackingEditStopTime_button);
    // using SimpleDateFormat class
    SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a",
            Locale.ENGLISH);
    String newStoptime = sdfStopTime
            .format(new Date(System.currentTimeMillis()));

    stopButton.append(newStoptime);

Many thanks for any help or advice on how to do this.

The way you are setting a time with new Date(System.currentTimeMillis()) currently is taking the exact, current millisecond and making a date out of it. If you insist on working with milliseconds, what you need to do is add an hour's worth of milliseconds or 1000 * 60 * 60 = 3600000.

Thus, Way #1 you can fulfill your needs is with this exact code:

Button stopButton = (Button) findViewById(R.id.StartTrackingEditStopTime_button);

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

String newStoptime = sdfStopTime.format(
        new Date(System.currentTimeMillis() + 3600000));

stopButton.setText(newStopTime);

This will work. Way #2 to accomplish this, and useful if you're systematically working with times, is to use the Calendar object. To do this, replace the third line from above with the following code:

Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 1);
Date d = c.getTime();
String newStopTime = sdfStopTime.format(d);

Hope this helps! Your choice.

This keeps everything in SimpleDateFormat. No need to create extra objects.

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

System.out.println("Before: " + sdfStopTime.getCalendar().getTime());

sdfStopTime.getCalendar().add(Calendar.HOUR, 1);

System.out.println("After: " + sdfStopTime.getCalendar().getTime());

The first argument of the add() method is the field, hours, minutes etc. The second argument is the amount you want to add, if negative you substract.

Use the Calendar class ( javadoc ). Assuming that you already have a Date now :

Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.HOUR, 1);
Date inAnHour = calendar.getTime();

// format inAnHour with your DateFormat and set a button label

You have to add one hour to the current time.

// You don't have to put System.currentTimeMillis() to the constructor.
// Default constructor of Date gives you the current time.
Date stopTime = new Date();
stopTime.setHours(stopTime.getHours() + 1);
String newStoptime = sdfStopTime.format(stopTime);
stopButton.append(newStoptime);

If you don't want to use deprecated functions setHours and getHours , use setTime and getTime and add 3,600,000 milli seconds (1 hour) to the current time:

stopTime.setTime(stopTime.getTime() + 60 * 60 * 1000);

instead of

stopTime.setHours(stopTime.getHours() + 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM