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.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Joanna
  • 76
  • 1
  • 7

1 Answers1

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

rohitvats
  • 1,811
  • 13
  • 11