-1

I cannot understand why does the program keep telling me I have a mistake 'The constructor Date() is undefined' in code line 4. I was using it in Spring Tool Suite,but that doesn't matter I guess. Please,can someone explain what to do and how. If it's possible write the correction and do explain. Thanks in advance!

FYI,I know it is probably a stupid question,but I am a beginner.

@RequestMapping("/date")
public String date () {
    DateFormat dateFormat = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);

}
Sonia Eaton
  • 3
  • 1
  • 3
  • 6
    Which `Date` are you importing? Or is this class or some other class in the package called `Date`? – Andy Turner Dec 15 '17 at 15:20
  • Date is depricated replaced by Calendar but still it should work. What "Date" do you import? Should be java.util.Date, right? – Pomagranite Dec 15 '17 at 15:27
  • @AndyTurner Do you mind hammering the dup? – user1803551 Dec 15 '17 at 15:30
  • Please search the site before asking questions. A search for "Java Date constructor" immediately gave the answer. – user1803551 Dec 15 '17 at 15:31
  • @user1803551 I've looked for it. As I've said in my question I'm a beginner,didnt even know what to search for and didnt see anything about imports. Generally didnt know imports can mess things up! – Sonia Eaton Dec 15 '17 at 15:37

2 Answers2

2

It seems that you have imported java.sql.Date; instead of java.util.Date;

Just change your import to :

import java.util.Date;

If you use eclipse be careful because when you organize your import the first proposed class is java.sql.Date

Guillaume Barré
  • 3,915
  • 2
  • 21
  • 46
1

You imported the wrong class.

Just check your header and replace import java.sql.Date; by import java.util.Date;

senerh
  • 1,185
  • 9
  • 18
  • These are the imports I have. import java.sql.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; – Sonia Eaton Dec 15 '17 at 15:27
  • 1
    @SoniaEaton Instead of `java.sql.Date` use `java.util.Date`. – user1803551 Dec 15 '17 at 15:28
  • Oh,thank you a lot! – Sonia Eaton Dec 15 '17 at 15:30