Results 1 to 2 of 2
  1. #1
    Rageagainst20 is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default Parsing Dates for Comparison

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
            // Get Date 1     
            try {
                    Date d1 = df.parse("2000-02-01");
            } catch(java.text.ParseException pe) {
                     JOptionPane.showMessageDialog(null, "Your parse got PWNT");
            }
            
            // Get Date 2
            try {
                   Date d2 = df.parse("2001-03-02");
            } catch(java.text.ParseException pe) {
                     JOptionPane.showMessageDialog(null, "Your parse got PWNT");
            }
    
    
        String relation;
        if (d1.equals(d2))
          relation = "the same date as";
        else if (d1.before(d2))
          relation = "before";
        else
          relation = "after";
        System.out.println(d1 + " is " + relation + ' ' + d2);
      
        }
    I am trying to comapre then display then in a text box using Try but it cannot read the varaibles because the operatin is outside the try is there anyway around this or a different way of doing this?

    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
        // Get dates
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = df.parse("2000-02-01");
            d2 = df.parse("2001-03-02");
        } catch(java.text.ParseException pe) {
            JptionPane.showMessageDialog(null, "parse error for: " +
                                                  pe.getMessage());
        }
    
        String relation;
        if (d1.equals(d2))
            relation = "the same date as";
        else if (d1.before(d2))
            relation = "before";
        else
            relation = "after";
        System.out.println(df.format(d1) + " is " + relation +
                           " " + df.format(d2));
    }

Similar Threads

  1. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 05:47 AM
  2. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 03:46 AM
  3. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 AM
  4. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 07:34 PM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01:16 AM

Posting Permissions

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