1

I am trying to store a previous date in a LocalDate object, but I am not sure how. I have been using my own class until now, but I would like to apply the java.time.LocalDate library. Help?

My code: LocalDate theDate = new LocalDate(theYear, theMonth, theDay);

Error message: LocalDate theDate = new LocalDate(theYear, theMonth, theDay);

2 Answers2

2

Try like this:

The Java 8 java.time.LocalDate class has no public constructors,

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);
    }



}
soorapadman
  • 4,123
  • 6
  • 31
  • 42
1

Try this-

LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
Kunal_89
  • 179
  • 1
  • 9
  • LocalDate theDate = new LocalDate(?); This is how far I have come. The questionmark represents my lack of knowledge. –  Jan 21 '16 at 07:42
  • I am looking for a way to enter an arbitrary date (yyyy-mm-dd) with the scanner. –  Jan 21 '16 at 07:45
  • @Erik If you look at the javadoc for [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html), you'll find [`LocalDate.parse(String text)`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-). I know, reading the doc is controversial, but you should try it. – Andreas Jan 21 '16 at 07:48
  • This is what I'm trying to do: LocalDate theDate = new LocalDate(int theYear, int theMonth, int theDay); Error message: The constructor LocalDate(int, int, int) is not visible –  Jan 21 '16 at 07:53