我可以在不使用 Java 中的日历库的情况下将秒数添加到当前日期吗?
Can I add seconds to current date without using Calendar Library in Java?
我想在Java中获取当前时间,现在我想在时间上面加上15秒。我可以在 java 中使用日历库 以外的库 执行此操作吗?
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("current date = " + calendar.getTime());
calendar.add(Calendar.SECOND, 15); // Add 15 seconds to current date
System.out.println("Updated Date = " + calendar.getTime());
}
}
确实如此! java.time
package was added because the Calendar
and Date
classes were insufficient. Something with LocalDateTime
也许吧。喜欢,
LocalDateTime now = LocalDateTime.now();
System.out.println("current date = " + now);
System.out.println("Updated Date = " + now.plusSeconds(15));
可以直接添加到java.util.Date,
Date now = new Date();
System.out.println("now : " + now);
long seconds = now.getTime();
seconds = seconds + (15 * 1000); //add 15 seconds * 1000 because in millis
Date then = new Date(seconds);
System.out.println("then : " + then);
我想在Java中获取当前时间,现在我想在时间上面加上15秒。我可以在 java 中使用日历库 以外的库 执行此操作吗?
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("current date = " + calendar.getTime());
calendar.add(Calendar.SECOND, 15); // Add 15 seconds to current date
System.out.println("Updated Date = " + calendar.getTime());
}
}
确实如此! java.time
package was added because the Calendar
and Date
classes were insufficient. Something with LocalDateTime
也许吧。喜欢,
LocalDateTime now = LocalDateTime.now();
System.out.println("current date = " + now);
System.out.println("Updated Date = " + now.plusSeconds(15));
可以直接添加到java.util.Date,
Date now = new Date();
System.out.println("now : " + now);
long seconds = now.getTime();
seconds = seconds + (15 * 1000); //add 15 seconds * 1000 because in millis
Date then = new Date(seconds);
System.out.println("then : " + then);