public int getMonths(GregorianCalendar g1, GregorianCalendar g2) {
   int elapsed = 0;
   GregorianCalendar gc1, gc2;
   if (g2.after(g1)) {
     gc2 = (GregorianCalendar) g2.clone();
     gc1 = (GregorianCalendar) g1.clone();
   } else {
     gc2 = (GregorianCalendar) g1.clone();
     gc1 = (GregorianCalendar) g2.clone();
   }
   gc1.clear(Calendar.MILLISECOND);
   gc1.clear(Calendar.SECOND);
   gc1.clear(Calendar.MINUTE);
   gc1.clear(Calendar.HOUR_OF_DAY);
   gc1.clear(Calendar.DATE);
   gc2.clear(Calendar.MILLISECOND);
   gc2.clear(Calendar.SECOND);
   gc2.clear(Calendar.MINUTE);
   gc2.clear(Calendar.HOUR_OF_DAY);
   gc2.clear(Calendar.DATE);
   while (gc1.before(gc2)) {
     gc1.add(Calendar.MONTH, 1);
     elapsed++;
   }
   return elapsed;
 }
  /**
   * Returns a java.util.Date object set to the time specified in newDate. The format expected is
   * the format of: YYYY-MM-DD HH:MM:SS
   *
   * @param newDate the string date to convert
   * @return A java.util.Date based off of the string format.
   */
  public static java.util.Date parseDate(String newDate) {
    // when we have a null from the database, give it zeros first.
    if (newDate == null || newDate.equals("")) {
      return null;
    }

    String parts[] = newDate.split(" ");
    String date[] = parts[0].split("/");
    String time[];

    if (parts.length > 1) {
      time = parts[1].split(":");
      time[2] = time[2].replaceAll("\\.0", "");
    } else {
      time = "00:00:00".split(":");
    }

    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    cal.clear();

    cal.set(
        Integer.parseInt(date[0]),
        (Integer.parseInt(date[1]) - 1),
        Integer.parseInt(date[2]),
        Integer.parseInt(time[0]),
        Integer.parseInt(time[1]),
        Integer.parseInt(time[2]));

    return cal.getTime();
  }
  /**
   * Parse <code><a href="http://www.w3.org/TR/xmlschema-2/#date">date</a></code> type:
   *
   * <p>_date = ["-"] 2*C 2Y "-" 2M "-" 2D
   */
  public static GregorianCalendar parseDate(String s) throws IOException {
    GregorianCalendar gc = new GregorianCalendar();
    gc.clear();

    try {
      String t = s;
      int i;

      if (t.startsWith("-")) {
        gc.set(GregorianCalendar.ERA, GregorianCalendar.BC);
        gc.set(GregorianCalendar.YEAR, Integer.parseInt(t.substring(1, i = t.indexOf("-", 1))));
      } else {
        gc.set(GregorianCalendar.ERA, GregorianCalendar.AD);
        gc.set(GregorianCalendar.YEAR, Integer.parseInt(t.substring(0, i = t.indexOf("-"))));
      }
      t = t.substring(i + 1);

      // Check delimiters (whos positions are now known).
      if (t.charAt(2) != '-') throw new IOException("Malformed date (" + s + ").");

      gc.set(GregorianCalendar.MONTH, Integer.parseInt(t.substring(0, 2)) - 1);
      t = t.substring(3);

      gc.set(GregorianCalendar.DAY_OF_MONTH, Integer.parseInt(t.substring(0, 2)));
    } catch (NumberFormatException nfe) {
      throw new IOException("Malformed date (" + s + ").");
    }

    return gc;
  }
 private String getFormattedDate(SimpleDate sd) {
   final SimpleDateFormat dateFrmt = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
   GregorianCalendar gc = new GregorianCalendar();
   gc.clear();
   gc.set(sd.getYear(), sd.getMonth(), sd.getDay());
   return dateFrmt.format(gc.getTime());
 }
示例#5
0
 public static Date createDate(int year, int month, int date) {
   GregorianCalendar calendar = new GregorianCalendar();
   calendar.clear();
   calendar.set(Calendar.YEAR, year);
   calendar.set(Calendar.MONTH, month - 1);
   calendar.set(Calendar.DAY_OF_MONTH, date);
   return calendar.getTime();
 }
  public void onDateSet(DatePicker view, int year, int month, int day) {

    Button dueDateText = (Button) findViewById(R.id.btCalendar);
    dueDateText.setText((month + 1) + "/" + day + "/" + year);
    GregorianCalendar gc = new GregorianCalendar();
    gc.clear();
    gc.set(year, month, day);
    dueDate = gc.getTimeInMillis();
  }
示例#7
0
 // Truncates the time component from a date/time value.
 // (The default time zone is used to determine the begin of the day).
 public Date truncateDate(Date d) {
   GregorianCalendar gc1 = new GregorianCalendar();
   gc1.clear();
   gc1.setTime(d);
   int year = gc1.get(Calendar.YEAR);
   int month = gc1.get(Calendar.MONTH);
   int day = gc1.get(Calendar.DAY_OF_MONTH);
   GregorianCalendar gc2 = new GregorianCalendar(year, month, day);
   return gc2.getTime();
 }
 @Override
 public void setValue(SimpleDate sd) {
   if (!sendClaim.isSelected()) {
     return;
   }
   GregorianCalendar gc = new GregorianCalendar();
   gc.clear();
   gc.set(GregorianCalendar.YEAR, sd.getYear());
   gc.set(GregorianCalendar.MONTH, sd.getMonth());
   gc.set(GregorianCalendar.DATE, sd.getDay());
   setClaimDate(gc.getTime());
 }
示例#9
0
  public void test(TestHarness harness) {
    GregorianCalendar cal;
    cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);

    // Month with 6 weeks
    cal.set(2006, Calendar.JULY, 30);
    cal.setLenient(false);

    harness.check(cal.getMaximum(Calendar.WEEK_OF_MONTH), 6);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 1);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 1);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 6);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 31);

    // Month with 4 weeks
    cal.set(1998, Calendar.FEBRUARY, 14);
    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 1);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 1);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 4);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 28);
    harness.check(cal.get(Calendar.MONTH), Calendar.FEBRUARY);

    // Month with 5 weeks
    cal.set(1993, Calendar.FEBRUARY, 14);
    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 5);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 28);
  }
示例#10
0
  /** @tests java.util.Date#parse(java.lang.String) */
  public void test_parseLjava_lang_String() {
    // Test for method long java.util.Date.parse(java.lang.String)
    Date d = new Date(Date.parse("13 October 1998"));
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(d);
    assertEquals("Parsed incorrect month", 9, cal.get(Calendar.MONTH));
    assertEquals("Parsed incorrect year", 1998, cal.get(Calendar.YEAR));
    assertEquals("Parsed incorrect date", 13, cal.get(Calendar.DATE));

    d = new Date(Date.parse("Jan-12 1999"));
    assertTrue("Wrong parsed date 1", d.equals(new GregorianCalendar(1999, 0, 12).getTime()));
    d = new Date(Date.parse("Jan12-1999"));
    assertTrue("Wrong parsed date 2", d.equals(new GregorianCalendar(1999, 0, 12).getTime()));
    d = new Date(Date.parse("Jan12 69-1"));
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.clear();
    cal.set(1969, Calendar.JANUARY, 12, 1, 0);
    assertTrue("Wrong parsed date 3", d.equals(cal.getTime()));
    d = new Date(Date.parse("6:45:13 3/2/1200 MST"));
    cal.setTimeZone(TimeZone.getTimeZone("MST"));
    cal.clear();
    cal.set(1200, 2, 2, 6, 45, 13);
    assertTrue("Wrong parsed date 4", d.equals(cal.getTime()));
    d = new Date(Date.parse("Mon, 22 Nov 1999 12:52:06 GMT"));
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.clear();
    cal.set(1999, Calendar.NOVEMBER, 22, 12, 52, 06);
    assertTrue("Wrong parsed date 5", d.equals(cal.getTime()));

    try {
      // Regression for HARMONY-259
      Date.parse(null);
      fail("Date.parse(null) should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
      // expected
    }
  }
示例#11
0
  public static XMLGregorianCalendar create(
      int year, int month, int day, int hr, int min, int sec) {
    GregorianCalendar cal = new GregorianCalendar(Locale.US);

    cal.clear();
    cal.setLenient(false);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, hr);
    cal.set(Calendar.MINUTE, min);
    cal.set(Calendar.SECOND, sec);

    return DATATYPE_FACTORY.newXMLGregorianCalendar(cal);
  }
示例#12
0
  /**
   * Converts a native value to an date value. If the value is a byte array and has the length of 8
   * a conversion is started. It is not checked whether the value contains only numbers.
   *
   * <p>For performance reasons no SimpleDateFormat is used. It is 4 times slower than a manual
   * convert.
   *
   * @returns a date.
   */
  public Date convertNativeToDate(tsColumn coldef, Object o) throws tinySQLException {
    if (o instanceof byte[]) {
      byte[] b = (byte[]) o;
      if (b.length == 10) {

        GregorianCalendar cal = new GregorianCalendar();
        cal.clear();

        int y = toNumber(b[0]) * 1000 + toNumber(b[1]) * 100 + toNumber(b[2]) * 10 + toNumber(b[3]);
        int m = toNumber(b[5]) * 10 + toNumber(b[6]);
        int d = toNumber(b[8]) * 10 + toNumber(b[9]);
        cal.set(y, m, d);
        return new java.sql.Date(cal.getTime().getTime());
      }
    }

    return super.convertNativeToDate(coldef, o);
  }
示例#13
0
  void checkCalendar(int start, int end) {
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    calendar.clear();

    calendar.set(Calendar.YEAR, start);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);

    Date date = new Date();
    date.setElapsedDays(DateUtil.getDayFromDate(start, 1, 1));
    while (calendar.get(Calendar.YEAR) <= end) {

      String calString = calendar.toString();
      assertThat("Year: " + calString, date.getYear(), is(calendar.get(Calendar.YEAR)));
      assertThat("Month: " + calString, date.getMonth(), is(calendar.get(Calendar.MONTH) + 1));
      assertThat("Date: " + calString, date.getDay(), is(calendar.get(Calendar.DATE)));

      calendar.add(Calendar.DATE, 1);
      date.setElapsedDays(date.getElapsedDays() + 1);
    }
  }
 /**
  * Change <code>date</code> so that it has no time information more accurate then the day level.
  * More accurate information is set to <code>0</code>.
  *
  * @result date != null ? result != date;
  * @result date == null ? result == null;
  * @result date != null ? result.getYear() == date.getYear();
  * @result date != null ? result.getMonth() == date.getMonth();
  * @result date != null ? result.getDay() == date.getDay();
  * @result date != null ? result.getHours() == 0;
  * @result date != null ? result.getMinutes() == 0;
  * @result date != null ? result.getSeconds() == 0;
  * @result date != null ? result.getMilliSeconds() == 0;
  * @mudo don't use deprecated methods in contract
  * @note This was difficult to get right. Don't change without tests!
  */
 public static Date dayDate(Date date) {
   if (date == null) {
     return null;
   }
   GregorianCalendar cal = new GregorianCalendar();
   cal.clear();
   cal.setTime(date);
   cal.clear(Calendar.HOUR);
   cal.clear(Calendar.HOUR_OF_DAY);
   cal.clear(Calendar.AM_PM);
   cal.clear(Calendar.MINUTE);
   cal.clear(Calendar.SECOND);
   cal.clear(Calendar.MILLISECOND);
   Date result = cal.getTime();
   return result;
 }
示例#15
0
  /**
   * converts a date to an byte-array with the the length of 8.
   *
   * @return a byte[] containing the date in the format "yyyyMMdd"
   */
  public Object convertDateToNative(tsColumn coldef, Date d) throws tinySQLException {
    try {
      GregorianCalendar cal = new GregorianCalendar();
      cal.clear();

      cal.setTime(d);
      int year = cal.get(cal.YEAR);
      int mon = cal.get(cal.MONTH);
      int day = cal.get(cal.DAY_OF_MONTH);
      Log.debug("Converting. DATE: " + d);
      StringBuffer b = new StringBuffer(10);
      b.append(Utils.forceToSizeLeft(String.valueOf(year), 4, '0'));
      b.append('-');
      b.append(Utils.forceToSizeLeft(String.valueOf(mon), 2, '0'));
      b.append('-');
      b.append(Utils.forceToSizeLeft(String.valueOf(day), 2, '0'));
      System.out.println("Quoting: " + quoting);
      return quoting.doQuoting(b.toString(), coldef.getSize()).getBytes(encoding);
    } catch (UnsupportedEncodingException e) {
      throw new tinySQLException("Encoding not supported");
    }
  }
  /**
   * Calculates the elapsed time between 2 dates. The elapsed time calculated could either be in
   * years, months or days
   *
   * @param type (int) The variable type determines the calculation of the elapsed time to be based
   *     on either years, months or days. To compute the elapsed time in year input type set to
   *     Calendar.YEAR To compute the elapsed time in month input type set to Calendar.MONTH By
   *     default the elapsed time will compute in days
   * @param startDate start date
   * @param endDate end date
   * @return the elapsed time (int)
   */
  public static int getElapsedTime(int type, Date startDate, Date endDate) {
    int elapsed = 0;

    if ((startDate == null) || (endDate == null)) {
      return -1;
    }

    if (startDate.after(endDate)) {
      return -1;
    }

    GregorianCalendar gc1 = (GregorianCalendar) GregorianCalendar.getInstance();
    GregorianCalendar gc2 = (GregorianCalendar) gc1.clone();
    gc1.setTime(startDate);
    gc2.setTime(endDate);

    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);

    if ((type != Calendar.MONTH) && (type != Calendar.YEAR)) {
      type = Calendar.DATE;
    }

    if (type == Calendar.MONTH) {
      gc1.clear(Calendar.DATE);
      gc2.clear(Calendar.DATE);
    }

    if (type == Calendar.YEAR) {
      gc1.clear(Calendar.DATE);
      gc2.clear(Calendar.DATE);
      gc1.clear(Calendar.MONTH);
      gc2.clear(Calendar.MONTH);
    }

    while (gc1.before(gc2)) {
      gc1.add(type, 1);
      elapsed++;
    }

    return elapsed;
  }