Tutorial System like azRank [Comparing dates]

Discussion in 'Resources' started by Plugers11, Feb 20, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Plugers11

    Hi guys!

    Today i wanna show u how to compare dates and use something like azRank system [maybe have another :c]


    I will try to explain you how to use this and make from this some useful things :>

    Let's start

    I will create return method named : getDaysBetweenTwoDates

    Code:
    public int getDaysBetweenTwoDates(){
    
    }
    Now we need to add parameters to compare :>

    We will use two GregorianCalendars
    Code:
    public int getDaysBetweenTwoDates(GregorianCalendar g1, GregorianCalendar g2) {
    
    }
    Now we create a counter integer

    Code:
    public int getDaysBetweenTwoDates(GregorianCalendar g1, GregorianCalendar g2) {
    int days = 0;
    }
    Now we create clones for that calendars, and check what's after and earlier [in parameters].

    Code:
    public int getDaysBetweenTwoDates(GregorianCalendar g1, GregorianCalendar g2) {
    int days = 0;
    GregorianCalendar gc1, gc2;
    if(g1.after(g2)){
       gc1 = (GregorianCalendar) g1.clone();
       gc2 = (GregorianCalendar) g2.clone();
    }else{
       gc1 = (GregorianCalendar) g2.clone();
       gc2 = (GregorianCalendar) g1.clone();
    }
    }
    Now we need to clear all from this calendars but not days. Need to use a loop.

    Code:
    public int getDaysBetweenTwoDates(GregorianCalendar g1, GregorianCalendar g2) {
    int days = 0;
    GregorianCalendar gc1, gc2;
    if(g1.after(g2)){
       gc1 = (GregorianCalendar) g1.clone();
       gc2 = (GregorianCalendar) g2.clone();
    }else{
       gc1 = (GregorianCalendar) g2.clone();
       gc2 = (GregorianCalendar) g1.clone();
    }
      gc1.clear(Calendar.MILLISECOND);
      gc1.clear(Calendar.SECOND);
      gc1.clear(Calendar.MINUTE);
      gc1.clear(Calendar.HOUR_OF_DAY);
      gc2.clear(Calendar.MILLISECOND);
      gc2.clear(Calendar.SECOND);
      gc2.clear(Calendar.MINUTE);
      gc2.clear(Calendar.HOUR_OF_DAY);
    
      while(gc1.before(gc2) ) {
         gc1.add(Calendar.DATE, 1);
         days++;
      }
      return days;
    }


    And that's all !
    If u have any questions just tell me :>


    Example use:

    Code:
    GregorianCalendar dd = new GregorianCalendar();
    GregorianCalendar dd2 = new GregorianCalendar(dd.getTime().getYear(), dd.getTime().getMonth() + 3, dd.getTime().getDay());
    int howmanydays = getDaysBetweenTwoDates(dd, dd2);
    Sorry for my english :D
     
    TigerHix likes this.
  2. Offline

    Assist

    @Plugers11
    Or just get two Dates and then

    Code:
    int daysBetween = (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
     
    TigerHix, Aqua, ReadySetPawn and 2 others like this.
Thread Status:
Not open for further replies.

Share This Page