-1

I would like to pass the current date into this method. Eventually I will have a button that I click and it will "Sell an animal"

public static void sellingAnimals(int animalschoosen) {
   pets.get(animalschoosen).setSellingDate();
}

In my head I think I may need to create a function and call that function in setSellingDate() brackets. How would I do this function? or is there a way of passing the current date in this method without a function?

3
  • 2
    You have a method setSellingDate, what does it do if it does not set the date? Or are you asking how to implement this method? Commented May 14, 2018 at 15:00
  • What you need is pets.get(animalschoosen).setSellingDate(LocalDate.now(yourTimeZone));. Commented May 15, 2018 at 9:45
  • Please search Stack Overflow before posting. You can assume that any basic question such as "getting today's date" has already been addressed in the 1.4 million questions/answers on Java here. Commented May 16, 2018 at 20:51

1 Answer 1

0

Yes, you can pass the current date in the method setSellingDate() without using any function.

use new Date() and to set it to currentDate. you can use the following snippet,

import java.text.SimpleDateFormat
String currentDateString = new SimpleDateFormat('yyyy-MM-dd').format(new Date())

Docs: https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date()

Sign up to request clarification or add additional context in comments.

1 Comment

Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.