Results 1 to 6 of 6
  1. #1
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default A problem with date formatting

    Hello. Does anyone know why the compiler dislikes this piece of code? To be more specific - it underlines only the last line (which starts with "String stD"), though it seems to be correct.
    I stopped the debugging with a breakpoint, and evaluated the expression "formatter1.format(formatter2.parse(data.toString( )))". The evaluation showed the value "1960-05-10" (like I wanted it to be). So what can be wrong here?

    Java Code:
    ......
     public void tableChanged(TableModelEvent e) {
           SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");
           SimpleDateFormat formatter2=new SimpleDateFormat("dd.MM.yyyy");
           int row = e.getLastRow();
           int column = e.getColumn();
           TableModel model = (TableModel)e.getSource();
           String columnName = model.getColumnName(column);
           Object data = model.getValueAt(row, column);
           String stD = formatter1.format(formatter2.parse(data.toString()));
    ......

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    How does the compiler dislike it? What is the exact error message yoiu get?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    it underlines the last line and shows the following hint: "unreported exception java.text.ParseException;must be caught or declared to be thrown"

  4. #4
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Well, I have rewritten the code like this:
    Java Code:
    ....
        public void tableChanged(TableModelEvent e) {
           SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");
           SimpleDateFormat formatter2=new SimpleDateFormat("dd.MM.yyyy");
           int row = e.getLastRow();
           int column = e.getColumn();
           TableModel model = (TableModel)e.getSource();
           String columnName = model.getColumnName(column);
           Object data = model.getValueAt(row, column);
           String stData=data.toString();
           Date d;
           try{
               d=formatter2.parse(stData);
               stData=formatter1.format(d);
           }
            catch(Exception x){ 
            System.out.println("Ошибка: " + x);
           };
    ....
    So the complier is happy now.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Bad. Don't catch Exception, catch the most appropriate subclass of Exception. The compiler error has already told you what exception you need to catch, but you should make it a habit to read the API.

    db

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    To add to Darryl's statement: at least print the stack trace.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. sql date problem
    By realosso in forum New To Java
    Replies: 3
    Last Post: 06-04-2010, 04:42 AM
  2. Date formatting
    By bikkerss in forum New To Java
    Replies: 13
    Last Post: 05-08-2010, 04:07 PM
  3. date problem
    By karlkwanny in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 10:50 AM
  4. Formatting time and date
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:35 PM
  5. Formatting the date
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-07-2007, 06:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •