How do I say 5 seconds from now in Java?

后端 未结 11 1659
青春惊慌失措
青春惊慌失措 2020-12-02 15:29

I am looking at the Date documentation and trying to figure out how I can express NOW + 5 seconds. Here\'s some pseudocode:

import java.util.Date
public clas         


        
相关标签:
11条回答
  • 2020-12-02 15:54

    I just found this from java docs

    import java.util.Calendar;
    
    public class Main {
    
      public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY) + ":"
            + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
    
        now.add(Calendar.SECOND, 100);
        System.out.println("New time after adding 100 seconds : " + now.get(Calendar.HOUR_OF_DAY) + ":"
            + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
      }
    }
    

    Is there a convention I should be aware of?

    0 讨论(0)
  • 2020-12-02 15:56

    UPDATE: See my new Answer using java.time classes. I am leaving this Answer intact as history.


    The Answer by Pascal Thivent and the Answer by Jon Skeet are both correct and good. Here's a bit of extra info.

    Five Seconds = PT5S (ISO 8601)

    Another way to express the idea of "five seconds later" is in a string using the standard formats defined by ISO 8601. The duration/period format has this pattern PnYnMnDTnHnMnS where the P marks the beginning and the T separates the date portion from time portion.

    So five seconds is PT5S.

    Joda-Time

    The Joda-Time 2.8 library can both generate and parse such duration/period strings. See the Period, Duration, and Interval classes. You can add and subtract Period objects to/from DateTime objects.

    Search StackOverflow for many examples and discussions. Here's one quick example.

    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
    DateTime now = DateTime.now( zone );
    DateTime then = now.plusSeconds( 5 );
    Interval interval = new Interval( now, then );
    Period period = interval.toPeriod( );
    
    DateTime thenAgain = now.plus( period );
    

    Dump to console.

    System.out.println( "zone: " + zone );
    System.out.println( "From now: " + now + " to then: " + then );
    System.out.println( "interval: " + interval );
    System.out.println( "period: " + period );
    System.out.println( "thenAgain: " + thenAgain );
    

    When run.

    zone: America/Montreal
    From now: 2015-06-15T19:38:21.242-04:00 to then: 2015-06-15T19:38:26.242-04:00
    interval: 2015-06-15T19:38:21.242-04:00/2015-06-15T19:38:26.242-04:00
    period: PT5S
    thenAgain: 2015-06-15T19:38:26.242-04:00
    
    0 讨论(0)
  • 2020-12-02 15:57

    Date is almost entirely deprecated and is still there for backward compatibility reasons. If you need to set particular dates or do date arithmetic, use a Calendar:

    Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
    calendar.add(Calendar.SECOND, 5);
    System.out.println(calendar.getTime());
    
    0 讨论(0)
  • 2020-12-02 16:02

    Try This..

        Date now = new Date();
        System.out.println(now);
    
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.SECOND, 5);
        now = c.getTime();
    
        System.out.println(now);
    
        // Output
        Tue Jun 11 16:46:43 BDT 2019
        Tue Jun 11 16:46:48 BDT 2019
    
    0 讨论(0)
  • 2020-12-02 16:03

    As others have pointed out, in Joda it's much easier:

    DateTime dt = new DateTime();
    DateTime added = dt.plusSeconds(5);
    

    I would strongly recommend you migrate to Joda. Almost any Java date-related question on SO resolves to a Joda recommendation :-) The Joda API is supposed to be the basis of the new standard Java date API (JSR310), so you'll be migrating towards a new standard.

    0 讨论(0)
提交回复
热议问题