multidimensional array convert to date java

416 Views Asked by At

I have:

int lineTable = Table.getRowCount();
        int columnTable= Table.getColumnCount();
        Object[][] arrayTable = new Object[lineTable ][columnTable];
        for (int j = 0; j  < lineTable ; j++) {
            for (int i = 0; i  < columnTable; i++) {
                arrayTable [j][i] = Table.getValueAt(j, i);
            }
        }

With this, I have a multidimensional array type Object with all data of the table... Can I convert this array to date(HH:mm)?

PS: The data is already in this format(HH:mm), but I don't know how take this directly in date format...

I did it! Just made these changes:

int lineTable = Table.getRowCount();
            int columnTable= Table.getColumnCount();
            Date[][] arrayTable = new Date[lineTable ][columnTable];
            for (int j = 0; j  < lineTable ; j++) {
                for (int i = 0; i  < columnTable; i++) {
                    arrayTable [j][i] = (Date)Table.getValueAt(j, i);
                }
            }

Thanks for all!

2

There are 2 best solutions below

3
On

Try something like this:

    public static void main(String[] args) throws ParseException {
        DateFormat df = new SimpleDateFormat("MM-dd-yyyy"); //your format here
        Date date = df.parse("11-3-1985");
        System.out.println("" + date);

    }

This will parse a date in the given format. You can also create another DateFormat for formatting the date when you print if if you want a different format example:

public static void main(String[] args) throws ParseException {
   DateFormat df = new SimpleDateFormat("MM-dd-yyyy"); //your format here
   DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
   Date date = df.parse("11-3-1985");
   System.out.println("" + df1.format(date));
}

This is just an example you can use whatever format you want...if you only want to give hours and seconds do that.

0
On

You can use the SimpleDateFormat class to parse the String to Date object like this:

 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
 String str = yourArray[x][x] + ":" + yourArray[x][x]
 Date date = sdf.parse(str);