-2

I would like to compare dates with before and after with the following code.

I have two variables varYEAR and varYEARDAY, and would like to compare them with "before" and "after".

If I save the code with Eclipse I got the following message:

The method before(String) is undefined for the type String

Sorry, I am a totally newcomer in java :-)

private void prepare_Data() throws Throwable {

    // Please implement me

    java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
    String date1 = df.format(varYEAR);

    java.text.DateFormat df1 = new java.text.SimpleDateFormat("yyyyMMdd");
    String date2 = df1.format(varYEARDAY);

    if(date1.**before**(date2)) {

        double dOAMT = Double.parseDouble(varOAMT);

        OP = OP + dOAMT;
        oOP = String.valueOf(OP);

    }

    if(date1.**after**(date2)){

         double dOAMT = Double.parseDouble(varOAMT);

         OPF = OPF+ dOAMT;
         oOPF = String.valueOf(OPF);

    }
}
Ben Green
  • 3,953
  • 3
  • 29
  • 49
kthink
  • 1
  • 1

3 Answers3

0

You dont have to convert your dates to spring. Date has before and after methods and can be compared to each other. You can use if(date2.after(date1)) or use date2.before(date1) depending upon your requirements.

You can find a lot of information as this question has been asked before How to compare dates in Java?

Community
  • 1
  • 1
user3509208
  • 531
  • 1
  • 5
  • 15
0
  • You can compare String using simply the method .compareTo(String)

This method requires (as you do) to write your strings with the format "yyyyMMdd" because it compares strings lexicographic value, but not what they actually mean.

This way, date1.compareTo(date2) returns

  1. a positive value (1) if date1 is greater than date2
  2. a negative value (-1) if date1 is smaller than date2
  3. 0 if they are equal

Then you write a bit of code to get your final result as a boolean. It actually works like a .after().

  • But there is much more efficient way to compare date in java : Date type

You have the java type Date. I guess your parameters varYEAR and varYEARDAY are already Date type. Methods .after() and .before() are already implemented and give you the result you are looking for.

varYEAR.after(varYEARDAY); // Boolean
  • If your parameters are not Date type, we will need their type to give you good answer. You can still construct a Date out of a string or a long containing your date in milliseconds.
    1. Date date1 = new SimpleDateFormat(Your_Format).parse(Your_Date_In_A_String);
    2. Date date1 = new Date(Your_Date_In_Milliseconds)
    3. If you want to build a Date using its calendar value (01/05/2016), you should use the GregorianCalendar type. (That has the same mathods .after() and .before()).

Use this constructor :

GregorianCalendar date1 = new GregorianCalendar(int year, int month, int day_of_month(1-31))

Have fun coding.

Charly
  • 282
  • 1
  • 12
0

Your Question is not clear and fails to document your inputs’ types and example values.

If, given your variables’ names, you originally have as inputs:

  • year number
  • year plus day-of-year (1-366)

…then you may be working too hard.

java.time

Instead of using the old date-time classes use their replacement, the java.time framework built into Java 8 and later.

Among these excellent classes is LocalDate. This class includes a factory method taking year and day-of-year.

int year = 2010;
LocalDate ld = LocalDate.ofYearDay( 2016 , 234 );
if ( ld.getYear() < year ) {
    …
}

If your year-day input is a String in standard ISO 8601 format as an ordinal date such as 2016-234, LocalDate can parse that with a formatter pre-defined as the constant DateTimeFormatter.ISO_ORDINAL_DATE.

LocalDate ld = LocalDate.parse( "2016-234" );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154