Date comparison


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Date comparison

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    1

    Date comparison

    Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    
    
    Date d1; 
    Date d2; 
    Date d3; 
    Date d4; 
    try { 
    d1 = df.parse("2007-01-01"); 
    d2 = df.parse("2007-04-01"); 
    d3 = df.parse("2001-07-02"); 
    d4 = df.parse("2001-09-02"); 
    } catch(java.text.ParseException pe) { 
    JOptionPane.showMessageDialog(null, "0"); 
    return; 
    } 
    
    String relation; 
    if (d1.before(d2)) 
    relation = "Date is in the first Quarter"; 
    else if (d1.after(d2)) 
    relation = "Date is in the second Quarter"; 
    else if (d2.before(d3)) 
    relation = "Date is in the second Quarter"; 
    else if (d2.after(d3)) 
    relation = "Date is in the third Quarter"; 
    else if (d3.before(d4)) 
    relation = "Date is in the third Quarter"; 
    else if (d3.after(d4)) 
    relation = "Date is in the third Quarter"; 
    else 
    relation = "Date is in the fourth quarter"; 
    TxtOutput.setText(d1 + " is " + relation + ' ' + d2 ); 
    
    
    }

    The above code detects the date for each variable and tells the user which quarter it is in displaying it in a text field.

    I have one question concerning...


    Code:
    d1 = df.parse("2007-01-01"); 
    d2 = df.parse("2007-04-01"); 
    d3 = df.parse("2001-07-02"); 
    d4 = df.parse("2001-09-02");

    Is it possible to have some sort of TO option for example


    Code:
    d1 = df.parse("2007-01-01 *TO 2007-03-31*");


    The idea is that i the end I will be able to compare todays date (the day in which the program is compiled i.e. not a a fixed day) with each quarter not just comparing quarters with each other, I also need help in this area too. Also is it possible to just select months instead of years bcause at present this program would only be compatible with this year instead of the "yyyy-MM-dd" format I could have a "MM-dd" format.

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    You cannot do such a parsing. The DateFormat object's parse method returns a Date. And a Date object can contain only one date, no range. All its fields except year will become 0 unless set otherwise. Year will be epoch (1970). This means you cannot compare only months and dates (though you can enter only month and date, year will be epoch till you set it). To get the current date, you can use new Date().

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Save yourself a lot of work by using the Calendar class. All the things you ask for here are already implemented in that class.
    eschew obfuscation

Similar Threads

  1. PLZ hELP ME IN.....DATE COMPARISON!
    By seanlaw in forum ASP.NET
    Replies: 4
    Last Post: 03-15-2007, 03:28 AM
  2. JAVA Date Utility Tutorial
    By hemanthjava in forum Java
    Replies: 0
    Last Post: 07-05-2006, 11:08 PM
  3. C#: Date Comparison
    By rkbnair in forum .NET
    Replies: 1
    Last Post: 05-23-2005, 12:31 PM
  4. Date Comparison
    By jbricher in forum ASP.NET
    Replies: 2
    Last Post: 04-10-2005, 01:37 PM
  5. Date and Time comparison
    By rtwitty in forum VB Classic
    Replies: 4
    Last Post: 02-24-2005, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center