2016-01-21 43 views
0

我想在LocalDate對象中存儲以前的日期,但我不知道如何。 我一直在使用我自己的類,但我想應用java.time.LocalDate庫。 幫助?用LocalDate存儲日期

我的代碼: LocalDate theDate = new LocalDate(theYar,theMonth,theDay);

錯誤消息: LocalDate theDate = new LocalDate(theYar,theMonth,theDay);

+1

請張貼一些代碼。 –

+0

「日期」是什麼意思? 'java.util.Date'? – MadProgrammer

+2

你是否在找這個?:http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate – soorapadman

回答

1

嘗試這樣的:

Java的8 java.time.LocalDate類沒有公共構造函數,

public class test { 
    public static void main(String[] args) { 
     int theYear=2016; 
     int theMonth=1; 
     int theDay=21; 
     LocalDate theDate = LocalDate.of(theYear,theMonth,theDay); 
     System.out.println(theDate); 
    } 



} 
+0

謝謝,它的工作! –

+0

@Erik不客氣 – soorapadman

0

嘗試這個 -

LocalDate today = LocalDate.now(); 
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); 
LocalDate yesterday = tomorrow.minusDays(2); 
+0

LocalDate theDate = new LocalDate(?); 這是我走了多遠。問號代表我缺乏知識。 –

+0

我正在尋找一種方法來使用掃描儀輸入任意日期(yyyy-mm-dd)。 –

+0

@Erik如果您查看['LocalDate']的javadoc(https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html),您會發現['LocalDate .parse(String text)'](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-)。我知道,閱讀文檔是有爭議的,但你應該嘗試。 – Andreas