Search code examples
javaintellij-14date

Adding current date to a method in YYYY-MM-DD


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?


Solution

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