How to compare year and month ? urgent ???


Web tier: servlets, JSP, Web frameworks: How to compare year and month ? urgent ???

  1. How to compare year and month ? urgent ??? (3 messages)

    Hi, I am using Mysql , and using DBtag so I can return the date from the database this form : 2000-10-13, I am interested to compare only the year and the month(2001 and 10 in this example) to the current year and month.

    Pleas help.
  2. String search="2000-10-10";
    String Hyphen="-";
    String result1="";
    String result2="";
    int i1;
    int Year=0;
    int Month=0;

    if(i1 != -1)
    Month =Integer.parseInt(result2);
    System.out.println("The Year and Month :" + result1 + ":" + result2);

    try this way,
  3. You can also simplify code by using StringTokenizer instead of substrings. This goes as follows:

    // first parameter is the datestring, second is the
    // delimiter
    StringTokenizer search = new StringTokenizer("2000-10-10", "-");
    String year = (String)search.nextElement();
    String month = (String)search.nextElement();
    String day = (String)search.nextElement();
    its that simple...
  4. Another way which will be useful if you need more advance date handling functionality is to use a SimpleDateFormat object, and use the parse method to create a Date object.
    The Date object will then allow you to do compare it with other dates, or set the time of a Calendar where you can do other operations.

    For instance,
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    Date parsed = sdf.parse( "1999-01-01" );
    boolean beforeNow = parsed.before( new Date( ) );

    Calendar c = Calendar.getInstance( );
    c.setTime( parsed );
    int month = c.getMonth( );