0

I have a class Sale, which looks like this

public class Sale extends Transaction
{
    //Attributes
    private static AtomicLong newSaleId = new AtomicLong();
    /**
     * The date of sale
     */
    private Date saleDate;
    /**
     * The sale id;
     */
    private long saleId;

    //Constructor

    Sale()
    {
        saleId = newSaleId.incrementAndGet();
        saleDate = 
    }
}

Constructor is not finished. Does anyone know how to set current date every time a new object is created? Thanks.

3
  • what about saleDate = new Date()? Commented Sep 3, 2016 at 10:38
  • beginnersbook.com/2013/05/current-date-time-in-java Commented Sep 3, 2016 at 10:39
  • You could also consider using LocalDate. i.e. LocalDate date = LocalDate.now(); (or LocalDateTime if you need the time too). Commented Sep 3, 2016 at 10:46

1 Answer 1

1

To set the current date, just instantiate a new Date object: saleDate = new Date()

See this JavaDoc for the Date() constructor for an explanation.

I will also encourage you to explore the new LocalDate and LocalDateTime classes introduced in Java 8, and here's an article explaining why they are better: Java 8 Date and Time API

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

1 Comment

Thank you ! I didn't know it's so simple.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.