0

i have a table in my database where i have a column with dates in format yyyy-MM-dd. I want to take the date from this table and to put it in a textfield in format dd/MM/yyyy.

I'm trying to use this but no success:

rsriga.next(); //the code is ok
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date invoiceDate = formatDate.parse(txtformatteddanasc.getText().trim());
java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());
String formattedDate = "";
formattedDate = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()).format(sqlDate);
txtformatteddanasc.setText(formattedDate);
user2272115
  • 986
  • 1
  • 10
  • 22
Captivesave
  • 69
  • 1
  • 11

1 Answers1

2

Try something like this

public static String sqlDateToString(java.sql.Date date){
    if(date != null) {
        java.util.Date utilDate = new java.util.Date(date.getTime());
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
        return dateFormat.format(utilDate);
    }
    return null;
}

Then call the method like this

while(resultSet.next()){
String sringFormatDate = className.sqlDateToString(resultSet.getDate("your_date_column"));
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • Thanks for the comment but if i use txtformatteddanasc.setText(sringFormatDate); then i get the date in my textfield in this format "dd-MM-yyyy", i would like to have "dd/MM/yyyy". – Captivesave Oct 03 '14 at 19:16
  • no it will be in the format "dd/MM/yyyy" as specified in `SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)` – SparkOn Oct 03 '14 at 19:20