Zeliax Zeliax - 4 months ago 13
Java Question

Using date, and date only, in java

I'm writing a java program and I have been stuck for quite some while with all the Date, DateTime and so forth parts of Java Programming.

What I want is to have a DATE object only. I have a class:

Deployment.java



public class Deployment {

int id;
LocalDate deviceDeploymentDate; //I'm unsure what this should be

public Deployment(int id, LocalDate deviceDeploymentDate) {
this.id = id;
this.deviceDeploymentDate = deviceDeploymentDate;
}

public Deployment() {

}

public LocalDate getDeviceDeploymentDate() {
return deviceDeploymentDate;
}

public void setDeviceDeploymentDate(LocalDate date) {
this.deviceDeploymentDate = date;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}


And I have a Date Picker in my GUI.

JDatePickerImpl

From this Date Picker I can get the date in a string format (and also change it to int year, int month and int date if needed ofc). From this I want to store some sort of DATE within my Deployment object, and I do not want the time to be part of this at all.

Question



Which datatype should I use to do this? And how would I do?

Misc



I have shortly looked at Joda Time, but have been unable to create a LocalDate / DateTime type from either the String or integers that I can get from the Date Picker.

Answer

You can keep using LocalDate(as per David's comment) and use DateTimeFormatter from Joda Time as below to convert String into LocalDate.

final DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MMM-dd");
final LocalDate dt = dtf.parseLocalDate(yourinputString);