This discussion is archived
1 2 Previous Next 25 Replies Latest reply: Jan 25, 2011 4:15 PM by DrClap Go to original post RSS
  • 15. Re: Convert string to date
    doremifasollatido Journeyer
    Currently Being Moderated
    EdChouaffe wrote:
    I didn't get the idea of looking up the toString method of the Class Date. It is all clear like water now.
    You're welcome. Looking up the Javadoc for a method or class when you don't understand why something is working the way it does should usually be the first thing you do in an attempt to understand what is going on.
  • 16. Re: Convert string to date
    YoungWinston Expert
    Currently Being Moderated
    EdChouaffe wrote:
    Thank you guys. You really helped me out. I didn't get the idea of looking up the toString method of the Class Date. It is all clear like water now.
    Good. However, to go back to your point about supporting various Locales, I'd advise you to re-read DrClap's first post.

    The code you've written ONLY supports date strings with the format you've shown; if any other machine/locale renders the date a different way, you'll be back to square one. My suggestion would be:
    (a) To make sure your managers understand this.
    (b) See if you can impose this format as a standard on your clients.

    Winston
  • 17. Re: Convert string to date
    EdChouaffe Newbie
    Currently Being Moderated
    Hi Guys, it is me again having problems with date parsing.

    Your previous answers could help pretty much, but there is another problem emerging now.
    The app I'm writing is in first place intended for the German market, so it has to run perfectly for the German Locale.
    The problem now is that I can't use or at least I couldn't so far parse the following String "Sat May 22 00:00:00 CEST 1993" with the format "EEE MMM dd HH:mm:ss zzz yyyy" when I set the dfault locale to german. Below is a sample code.

    Locale.setDefault(new Locale("de"));

              DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
              String obj = "Sat May 22 00:00:00 CEST 1993";
              Date date = null;
              try {
                   date = df.parse(obj.toString());
              } catch (ParseException e) {
                   e.printStackTrace();
              }

    System.out.println("Parsed date: " + date);

    It only works for the English Locale.
    Thanks.
  • 18. Re: Convert string to date
    796440 Guru
    Currently Being Moderated
    Are "Sat" and "May" valid in German? If not, why would you expect it to parse that String in the German Locale?

    Also note that SDF has a constructor that takes a Locale, so you can parse dates in a format outside your default Locale.
  • 19. Re: Convert string to date
    796440 Guru
    Currently Being Moderated
    date = df.parse(obj.toString());
    Also note that the toStriing() here, while harmless, is redundant, since obj is already a String.
  • 20. Re: Convert string to date
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    EdChouaffe wrote:
    The app I'm writing is in first place intended for the German market, so it has to run perfectly for the German Locale.
    Perhaps the real problem is that you do not know what that locale actually looks like?

    If so getting an example would certainly help with testing.

    And it is going to be difficult to actually deliver to the "German market" if you do not actually have a machine which is set up to mimic what the "German market" looks like.
    "Sat May 22 00:00:00 CEST 1993"
    Why is that a valid format for parsing anyways?

    Users are not going to type that format.
    Database usage is irrelevant.
    A well designed import/export file is not going to use that format.

    So where are you getting that format from?
  • 21. Re: Convert string to date
    DrClap Expert
    Currently Being Moderated
    jschell wrote:
    "Sat May 22 00:00:00 CEST 1993"
    Why is that a valid format for parsing anyways?

    Users are not going to type that format.
    Database usage is irrelevant.
    A well designed import/export file is not going to use that format.

    So where are you getting that format from?
    The OP already said:
    Here is the full story.
    I'm writing a cell editor for jtable cells which have Date as type. The postgres database by default use an instance of Dateformat with SHORT style to format the dates display. Sofar so good. The problem appears when I click on the cell. The returned value is no longer in the SHORT style but in the format I gave above. To further process with the value, I strongly need to parse it back to a date in the SHORT or Medium style.
    To which I responded that the database was irrelevant. That doesn't seem to have had any effect though. It appears that the OP is getting dates from the database and then just allowing Date.toString() to format them based on whatever locale happens to be in effect. Hence the perceived problem, which would be solved simply by formatting them with a suitable format instead of being at the mercy of an unexpected locale choice. Sometimes it's an uphill battle weaning people to the simple solution when they are overly attached to the complicated one.
  • 22. Re: Convert string to date
    796440 Guru
    Currently Being Moderated
    DrClap wrote:
    jschell wrote:
    "Sat May 22 00:00:00 CEST 1993"
    Why is that a valid format for parsing anyways?

    Users are not going to type that format.
    Database usage is irrelevant.
    A well designed import/export file is not going to use that format.

    So where are you getting that format from?
    The OP already said:
    Here is the full story.
    I'm writing a cell editor for jtable cells which have Date as type. The postgres database by default use an instance of Dateformat with SHORT style to format the dates display. Sofar so good. The problem appears when I click on the cell. The returned value is no longer in the SHORT style but in the format I gave above. To further process with the value, I strongly need to parse it back to a date in the SHORT or Medium style.
    To which I responded that the database was irrelevant. That doesn't seem to have had any effect though. It appears that the OP is getting dates from the database and then just allowing Date.toString() to format them based on whatever locale happens to be in effect.
    Oh, is that what he meant? I couldn't really decipher what he was saying, so I just went along with it as if it made sense.

    @OP: As already pointed out: If you're getting a Date from a DB, and if the column is a SQL DATE or DATETIME column, then there's no reason to even deal with parsing date formats at all, because, as already mentioned several times Dates do not have formats. The only reason you'd care about formats in this case is for displaying the date to the user, and that would be a call to format(), not parse(). You would definitely NOT then re-parse() that string back into a Date. If that's what you're doing, you're doing it wrong.

    The only reason to deal with a format for a date coming from a DB is if the date is stored in a textual column, such as VARCHAR (which would be a major design flaw). If that's the case, then you must know ahead of time what format and locale that string (NOT date) is using. And if there's more than one possible format or locale, you must know all the possibilities, and there must be no ambiguity between them. For instance, if both MM/dd/yyyy and dd/MM/yyyy are possible, then you cannot correctly parse that string (NOT date) into a Date.

    Edited by: jverd on Jan 25, 2011 1:17 PM
  • 23. Re: Convert string to date
    DrClap Expert
    Currently Being Moderated
    jverd wrote:
    To which I responded that the database was irrelevant. That doesn't seem to have had any effect though. It appears that the OP is getting dates from the database and then just allowing Date.toString() to format them based on whatever locale happens to be in effect.
    Oh, is that what he meant? I couldn't really decipher what he was saying, so I just went along with it as if it made sense.
    That was my interpretation, but as you imply, there wasn't a really good explanation of the background. It's also possible that he is getting Date variables from the result set using the getString() method, thus allowing the database to format the date as a string. At any rate we're still being given symptoms and proposed solutions to those symptoms instead of cleaning up the fundamental problem... whatever that is. I'm sure there is one which isn't being talked about.
  • 24. Re: Convert string to date
    doremifasollatido Journeyer
    Currently Being Moderated
    DrClap wrote:
    jverd wrote:
    To which I responded that the database was irrelevant. That doesn't seem to have had any effect though. It appears that the OP is getting dates from the database and then just allowing Date.toString() to format them based on whatever locale happens to be in effect.
    Oh, is that what he meant? I couldn't really decipher what he was saying, so I just went along with it as if it made sense.
    That was my interpretation, but as you imply, there wasn't a really good explanation of the background. It's also possible that he is getting Date variables from the result set using the getString() method, thus allowing the database to format the date as a string. At any rate we're still being given symptoms and proposed solutions to those symptoms instead of cleaning up the fundamental problem... whatever that is. I'm sure there is one which isn't being talked about.
    I think the problem is that his TableModel, CellEditor, and/or TableCellRenderer isn't written properly. If you use a DefaultTableModel, a DefaultCellEditor, and a DefaultTableCellEditor, you can add any object that you want, and the Object's "toString()" method will be used to display it. If you want to display it some other way, you need to customize one or more of those pieces (the TableModel, the CellEditor, or the TableCellRenderer). The OP said he was trying to write a cell editor. He probably should write a cell editor that uses a JFormattedTextField with appropriate formatters to keep the String format of the Date the way he wants while editing and while not editing. The getValueAt(row, column) for the OP's TableModel should return a Date, not a String. getValueAt() is declared to return an Object, but the actual runtime type of that Object should be Date, not String.
  • 25. Re: Convert string to date
    DrClap Expert
    Currently Being Moderated
    doremifasollatido wrote:
    I think the problem is that his TableModel, CellEditor, and/or TableCellRenderer isn't written properly. If you use a DefaultTableModel, a DefaultCellEditor, and a DefaultTableCellEditor, you can add any object that you want, and the Object's "toString()" method will be used to display it. If you want to display it some other way, you need to customize one or more of those pieces (the TableModel, the CellEditor, or the TableCellRenderer). The OP said he was trying to write a cell editor. He probably should write a cell editor that uses a JFormattedTextField with appropriate formatters to keep the String format of the Date the way he wants while editing and while not editing. The getValueAt(row, column) for the OP's TableModel should return a Date, not a String. getValueAt() is declared to return an Object, but the actual runtime type of that Object should be Date, not String.
    Very likely true. Again that's the underlying thing which should be fixed instead of focusing on the superficial things which look like they should be fixed.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points