Date object to Calendar [Java]

后端 未结 7 1958
花落未央
花落未央 2020-12-02 19:31

I have a class Movie in it i have a start Date, a duration and a stop Date. Start and stop Date are Date Objects (private Date startDate ...) (It\'s an assignment so i cant

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 20:17

    You don't need to convert to Calendar for this, you can just use getTime()/setTime() instead.

    getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

    setTime(long time) : Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. )

    There are 1000 milliseconds in a second, and 60 seconds in a minute. Just do the math.

        Date now = new Date();
        Date oneMinuteInFuture = new Date(now.getTime() + 1000L * 60);
        System.out.println(now);
        System.out.println(oneMinuteInFuture);
    

    The L suffix in 1000 signifies that it's a long literal; these calculations usually overflows int easily.

提交回复
热议问题