Solved

Comparing dates in java

Posted on 2011-09-28
11
243 Views
Last Modified: 2012-05-12
Hi Experts...
I have a number of dates in format of MMMDDYYYY and i have to compare if the date falls under a particular range i.e if SEP202011 falls under SEP152011 and SEP242011.

Basically i read the dates to be compared from a file which will be retreived in string format which will be a list of them to compare.

I have to perfrom this comparison for a number of dates in this format if they fall under this limit.
How do i do this.
Please help...
0
Comment
Question by:gaugeta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36802847


SimpleDateFormat sdf = new SimpleDateFormat("MMMddyyyy");
java.util.Date dd = sdf.parse("SEP202011", new ParsePosition(0));
java.util.Date dd1 = sdf.parse("SEP152011", new ParsePosition(0));


if(dd1.before(dd)){do something}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36805004
This is the test:

SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy");
java.util.Date dd = sdf1.parse("SEP202011", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse("SEP152011", new ParsePosition(0));
  java.util.Date dd2 = sdf1.parse("SEP242011", new ParsePosition(0));


        if(dd1.before(dd) && dd2.after(dd)){
            System.out.println(dd1 + " before " + dd +"  and  " + dd2 + "   after " + dd);
        }    

Open in new window


Output:
Thu Sep 15 00:00:00 PDT 2011 before Tue Sep 20 00:00:00 PDT 2011  and  Sat Sep 24 00:00:00 PDT 2011   after Tue Sep 20 00:00:00 PDT 2011

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36805078
@for_yan:Here i have to compare if a date falls between a range.

I think if(dd1.before(dd)){do something} is comparing if dd1 occurs before dd then i can take any action.

How do i modify this to compare within a date range.
Please help...
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:gaugeta
ID: 36805849
@for_yan:And i have to check inclusive of the date range as well.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36805850

   look above :
if(dd1.before(dd) && dd1.after(dd2))  - this would mean that dd1 is between dd and dd2
0
 

Author Comment

by:gaugeta
ID: 36806542
@for_yan:Sorry didnt see that.
How do i check inclusive of the date range as well.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36808170
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");
        String s0 =  "SEP152011";
        String s2 = "SEP242011";
        String s1 =  "SEP202011";
        
        
        
java.util.Date dd0 = sdf1.parse(s0 + " 00:00:01", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse(s1 + " 00:00:00", new ParsePosition(0));
  java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));

              if(dd1.getTime() > dd0.getTime() && dd1.getTime()<dd2.getTime()){
                  System.out.println("true");
                  
              }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36809069
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");

SimpleDateFormat sdf2 = new SimpleDateFormat("MMMddyyyy");
        String s0 =  "SEP152011";
        String s2 = "SEP242011";
        String s1 =  "SEP202011";



java.util.Date dd0 = sdf1.parse(s0 + " 00:00:01", new ParsePosition(0));
java.util.Date dd1 = sdf2.parse(s1, new ParsePosition(0));
  java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));

              if(dd1.getTime() > dd0.getTime() && dd1.getTime()<dd2.getTime()){
                  System.out.println("true");

              }

Open in new window

Output:
true

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36810964

I think this would be better - this is truly inclusive:
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");


        String s0 =  "SEP152011";
        String s2 = "SEP242011";
        String s1 =  "SEP152011";



java.util.Date dd0 = sdf1.parse(s0 + " 00:00:00", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse(s1 + " 12:00:00", new ParsePosition(0));
  java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));

              if(dd1.getTime() >= dd0.getTime() && dd1.getTime() <= dd2.getTime()){
                  System.out.println("true");

              }

Open in new window


Output
true

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36812236
@for_yan:Thanks a lot.
Will try this and get back to you if i face any issues.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36812640
In fact this is simpler - but it is also inclusive - gives true if d1 is 24th or if d1 is 15th of september

SimpleDateFormat sdf2 = new SimpleDateFormat("MMMddyyyy");
        String s0 =  "SEP152011";
        String s2 = "SEP242011";
        String s1 =  "SEP242011";



java.util.Date dd0 = sdf2.parse(s0 , new ParsePosition(0));
java.util.Date dd1 = sdf2.parse(s1 , new ParsePosition(0));
  java.util.Date dd2 = sdf2.parse(s2 , new ParsePosition(0));

              if(dd1.getTime() >= dd0.getTime() && dd1.getTime() <= dd2.getTime()){
                  System.out.println("true");

              }

Open in new window


true

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
iterator/ListIterator approach 17 53
Java program running SQL query 5 48
web project error add remove 1 52
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question