-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?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
High5
  • 3
  • 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? – Ben May 14 '18 at 15:00
  • What you need is `pets.get(animalschoosen).setSellingDate(LocalDate.now(yourTimeZone));`. – Ole V.V. May 15 '18 at 09:45
  • Please search Stack Overflow before posting. You can assume that any basic question such as ["getting today's date"](https://stackoverflow.com/search?q=java+current+date) has already been addressed in the [1.4 *million* questions/answers on Java](https://stackoverflow.com/questions/tagged/java) here. – Basil Bourque May 16 '18 at 20:51

1 Answers1

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()

Shivam Pandey
  • 3,756
  • 2
  • 19
  • 26
  • 2
    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](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. May 15 '18 at 09:24