1

I try to store the current time and date but get the following error:

incompatible types: String cannot be converted to Date

This is the code:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();

time.setUpdateDt(dtf.format(now));

public Date getUpdateDt() {
    return time;
}

 public void setUpdateDt(Date time) {
     this.time = time;
}
Andreas
  • 5,393
  • 9
  • 44
  • 53
Tony
  • 2,515
  • 14
  • 38
  • 71
  • 4
    [`DateTimeFormatter.format`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#format-java.time.temporal.TemporalAccessor-) return a `String` but `setUpdateDt` accept a `Date` .. also don't mix `LocalDateTime` and `Date`, two distinct time API. Keep the most recent one, `java.time`. – AxelH Mar 21 '18 at 06:50
  • 1
    Possible duplicate of [Converting between java.time.LocalDateTime and java.util.Date](https://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date) – AxelH Mar 21 '18 at 07:25
  • Best if you can change the class of your `time` object to accept a `LocalDateTime` (or other type from java.time, the modern Java date and time API) so you can avoid the long outdated `Date` class completely. – Ole V.V. Mar 21 '18 at 09:26

4 Answers4

1

You are using a DateTimeFormatter to transform a LocalDateTime into a String with DateTimeFormatter.format. Then you send that result as a parameter to setUpdateDt.

public void setUpdateDt(Date time)

The problem is that you send a String instead of a Date.

FYI: you are mixing the time technologies here. Date and LocalDateTIme are two distinct time API, you should not mix those. If you want your POJO to store a date, store it as LocalDateTime.

public void setUpdateDt(LocalDateTime time)

If you really want to get it as a Date from the LocalDateTime, check this answer: Converting between java.time.LocalDateTime and java.util.Date

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

dtf.format(now) will return a String. You are getting the incompatible type error since you are trying to pass this value to the setter which accepts a Date object. Instead of time.setUpdateDt(dtf.format(now));

You could use the java.util.Date class to create a new Date as follows -

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Date now = new Date();

time.setUpdateDt(dtf.format(now));

public Date getUpdateDt() {
    return time;
}

public void setUpdateDt(Date time) {
    this.time = time;
}

Or, if you want use the existing LocalDateTime, you can convert it to Date by -

LocalDateTime now = LocalDateTime.now();
Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
time.setUpdateDt(dtf.format(date));
Ameya Pandilwar
  • 2,638
  • 28
  • 28
0

dtf.format(now) returns a string. not a date object.

if you just want to convert from LocalDate to Date, you could use this method:

LocalDateTime now = LocalDateTime.now();
Date yourDate = Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant());

however if you still need to parse a string, you could inject a parser and assign a date object in your class.

for example:

public class Time {
    Date time;
     public Date getUpdateDt() {
            return time;
        }

        public void setUpdateDt(Date time) {
            this.time = time;
        }
        //this method will accept a string and inject a date format parser
        public void setUpdateDt(String timeStr,DateTimeFormatter dtf) {
            LocalDateTime datetime =LocalDateTime.parse(timeStr, dtf);

            this.time = Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant());

        }

    public static void main(String args[]) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime now = LocalDateTime.now();
        Time time = new Time();
        time.setUpdateDt(dtf.format(now),dtf);
        System.out.println(time.getUpdateDt());


    }
dsncode
  • 2,407
  • 2
  • 22
  • 36
0
initComponents();

jDateChooser.setDate(new Date(fecha()));

//Formato de Fecha public String fecha() {

    Date fecha=new Date();   
    SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/YYYY", new Locale("es", "EC"));
    return formatoFecha.format(fecha);
}
  • 1
    Thanks for wanting to contribute. The OP is wisely using java.time, the modern Java date and time API (`DateTimeFormatter` and `LocalDateTime`). So please do not suggest using `Date` and `SimpleDateFormat`. They are not only troublesome because of bad design, they are also long outdated. – Ole V.V. Apr 07 '22 at 05:33