该代码段使用Java 8中的日期时间API,首先获取当前日期时间,然后使用plusHours()方法将其加上24小时。接着,使用DateTimeFormatter将日期时间格式化为字符串,并将结果打印出来。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Add24Hours {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now(); //获取当前日期时间
LocalDateTime after24hours = now.plusHours(24); //日期时间加上24小时
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //格式化日期时间
String formattedDateTime = after24hours.format(formatter); //将日期时间转换为字符串
System.out.println("24小时后的日期时间为:" + formattedDateTime); //打印结果
}
}