2017-05-27 56 views
0

以指定格式輸入日期作爲輸入。 我如何獲得日,月和年,並將它們包含在三個不同的變量中? 我試圖得到年(),但它給了我一個錯誤,因爲它已經過時,我讀了你可以用「日曆」,但我不知道如何在這個庫從日期類型中提取年,月和日

System.out.println("Inserisci la data [yyyy-MM-dd]: "); 
Scanner in = new Scanner(System.in); 
s = in.nextLine(); 
//converte la stringa della data in un oggetto di classe Date 
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd"); 
Date d = formato.parse(s); 

回答

0
Calendar c = new Calendar(); 
c.setTime(yourDate); 
int year = c.get(Calendar.YEAR); 
int month = c.get(Calendar.MONTH); 
int day = c.get(Calendar.DAY_OF_MONTH); 
使用