Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:22:37+00:00 2026-06-07T20:22:37+00:00

While I retrieve a date field from access database and show it in JTable,

  • 0

While I retrieve a date field from access database and show it in JTable, it is in yyyy-MM-dd format. I want it to be dd-MM-yyyy. Though I changed the format in MS Access, while it is shown in panel it is in yyyy-MM-dd only. How can I change it?

The date field is one of the many fields I retrieve with a query. So, I don’t understand where to put the SimpleDateFormatter and format it for each row. Kindly help me out. Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Date;
import java.sql.*;
import java.sql.ResultSetMetaData;
import java.text.*;

public class gc implements ActionListener
{
JComboBox cc=new JComboBox();
JFrame frame=new JFrame();
JTable table;
DefaultTableModel model;
String query;
int i;
JPanel panel=new JPanel();

public gc()
{
frame.setTitle("Composition Check");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());

//Locale locale = Locale.getDefault();
//  System.out.println("Before setting, Locale is = " + locale); 
//Locale.setDefault(Locale.English);
//System.out.println("Before setting, Locale is = " + locale); 

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select DISTINCT composition from try");

while(rs.next())
{
 cc.addItem(rs.getString("composition"));
}

conn.close();
}
catch(Exception e)
{
System.out.println(e);
}

p1.add(cc);
cc.addActionListener(this);
frame.add(p1,BorderLayout.NORTH);

frame.setVisible(true);
}

public class MyTableModel extends DefaultTableModel
{
public Class getColumnClass(int col) 
{
    if (col == 3) 
    {
        return java.util.Date.class;
    }
}
}

public void addTable(String query)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st=conn.createStatement();
System.out.println(query);
ResultSet rs=st.executeQuery(query);
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();
//table=new JTable();
model=new DefaultTableModel();

model.addColumn("Purpose");
model.addColumn("Name");
model.addColumn("Manu");
model.addColumn("Expiry");
model.addColumn("Stock");
model.addColumn("Cost");
model.addColumn("Supplier");
model.addColumn("Supplier Number");
model.addColumn("Rack");

table=new JTable(new MyTableModel());

String[] tabledata=new String[cols];
int i=0;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

while(rs.next())
{
for(i=0;i<cols;i++)
{
//if(i==3)
//{
 //System.out.println(rs.getObject(i+1).toString()); 
// tabledata[i]=formatter.format(rs.getObject(i+1).toString());
//}

 tabledata[i]=rs.getObject(i+1).toString();

}
model.addRow(tabledata);

}

panel.removeAll();
JScrollPane scroll = new JScrollPane(table); 
panel.setLayout(new BorderLayout());

panel.add(scroll,BorderLayout.CENTER);
frame.add(panel,BorderLayout.CENTER);
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
}

public void actionPerformed(ActionEvent evt)
{
String ac=(String)cc.getSelectedItem();
System.out.println(ac);

addTable("select * from try where composition='"+ac+"'");
frame.setVisible(true);
}

public static void main(String[] args)
{
new gc();
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:22:39+00:00Added an answer on June 7, 2026 at 8:22 pm

    You need to customize the table model to tell the JTable that the column is holding values of type Date:

    @Override
    public Class getColumnClass(int col) {
        if (col == 3) {
            return java.util.Date.class;
        }
        // return the appropriate class for every column
    }
    

    This will make the JTable use a renderer formatting the date with the format associated to the current locale. If you need another format, you need to associate another renderer to the column, which formats the date as you want to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I retrieve a list of date from a DB using JS and ADO and
i want to retrieve data which their price field is between a certain max
I am using JDateChooser from here However I can not retrieve the date in
In my iPhone App I want to retrieve image from (stored in ) sqlite
Hey I got a problem here. I'm trying to retrieve the date from the
I am trying to retrieve the date only from my table and store it
While using ivy:retrieve , it fails to resolve the dependency that should be downloaded.
While it is clear we cannot retrieve a list of installed applications on iOS,
I wanted to convert DateTime data which will be pulled from source, to Date
I am currently using a date and time picker to retrieve a user-submitted date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.