당신은 LocalDateTime 대신 사용할 수 있습니다
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.Random;
class Main {
public static void main(String[] args) {
// Declare DateTimeFormatter with desired format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
// Save current LocalDateTime into a variable
LocalDateTime localDateTime = LocalDateTime.now();
// Format LocalDateTime into a String variable and print
String formattedLocalDateTime = localDateTime.format(dateTimeFormatter);
System.out.println("Current Date: " + formattedLocalDateTime);
//Get random amount of days between 5 and 10
Random random = new Random();
int randomAmountOfDays = random.nextInt(10 - 5 + 1) + 5;
System.out.println("Random amount of days: " + randomAmountOfDays);
// Add randomAmountOfDays to LocalDateTime variable we defined earlier and store it into a new variable
LocalDateTime futureLocalDateTime = localDateTime.plusDays(randomAmountOfDays);
// Format new LocalDateTime variable into a String variable and print
String formattedFutureLocalDateTime = futureLocalDateTime.format(dateTimeFormatter);
System.out.println("Date " + randomAmountOfDays + " days in future: " + formattedFutureLocalDateTime);
}
}
예 출력 : 이미 변수에 현재 날짜를 저장하는
Current Date: 2017/11/22 20:41:03
Random amount of days: 7
Date 7 days in future: 2017/11/29 20:41:03
합니다. 'new Date()'는 현재 날짜,'Date date'는 변수,'Date date = new Date();'는 변수에 현재 날짜를 저장합니다. –
그리고 지금까지 해본 것은 무엇입니까? – assembler