示例#1
0
  protected int[] getNow(int[] n) {
    Calendar cal = Calendar.getInstance();
    if (n[0] != NULL && n[0] != IGN) cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + n[0]);
    if (n[1] != NULL && n[1] != IGN) cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + n[1]);
    if (n[2] != NULL && n[2] != IGN)
      cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n[2]);
    if (n[3] != NULL && n[3] != IGN)
      cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + n[3]);
    if (n[4] != NULL && n[4] != IGN) cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + n[4]);
    if (n[5] != NULL && n[5] != IGN) cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + n[5]);

    return new int[] {
      n[0] == NULL ? 0 : (n[0] == IGN ? IGN : cal.get(Calendar.YEAR)),
      n[1] == NULL ? 0 : (n[1] == IGN ? IGN : cal.get(Calendar.MONTH)),
      n[2] == NULL ? 0 : (n[2] == IGN ? IGN : cal.get(Calendar.DAY_OF_MONTH)),
      n[3] == NULL ? 0 : (n[3] == IGN ? IGN : cal.get(Calendar.HOUR_OF_DAY)),
      n[4] == NULL ? 0 : (n[4] == IGN ? IGN : cal.get(Calendar.MINUTE)),
      n[5] == NULL ? 0 : (n[5] == IGN ? IGN : cal.get(Calendar.SECOND))
    };
  }
  @Test
  public void testConvertStringToTimestamp() throws Exception {
    final Date today = new Date();
    final Calendar todayCalendar = new GregorianCalendar();
    todayCalendar.setTime(today);
    final String datePart = DateUtil.convertDateToString(today);

    final Timestamp time = (Timestamp) converter.convert(Timestamp.class, datePart + " 01:02:03.4");
    final Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time.getTime());
    assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR));
    assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH));
    assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
  }
  @Test
  public void testConvertStringToDate() throws Exception {
    final Date today = new Date();
    final Calendar todayCalendar = new GregorianCalendar();
    todayCalendar.setTime(today);
    final String datePart = DateUtil.convertDateToString(today);
    // test empty time
    Date date = (Date) converter.convert(Date.class, "");
    assertNull(date);
    date = (Date) converter.convert(Date.class, datePart);

    final Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR));
    assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH));
    assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
  }
示例#4
0
 protected Collection compare(Date date, int[] fields) {
   List result = new ArrayList();
   Calendar cal = Calendar.getInstance();
   cal.setLenient(true);
   cal.setTime(date);
   if (fields[0] != IGN && cal.get(Calendar.YEAR) != fields[0])
     result.add("year " + cal.get(Calendar.YEAR) + " != " + fields[0]);
   if (fields[1] != IGN && cal.get(Calendar.MONTH) != fields[1])
     result.add("month " + cal.get(Calendar.MONTH) + " != " + fields[1]);
   if (fields[2] != IGN && cal.get(Calendar.DAY_OF_MONTH) != fields[2])
     result.add("day of month " + cal.get(Calendar.DAY_OF_MONTH) + " != " + fields[2]);
   if (fields[3] != IGN && cal.get(Calendar.HOUR_OF_DAY) != fields[3])
     result.add("hour of day " + cal.get(Calendar.HOUR_OF_DAY) + " != " + fields[3]);
   if (fields[4] != IGN && cal.get(Calendar.MINUTE) != fields[4])
     result.add("minute " + cal.get(Calendar.MINUTE) + " != " + fields[4]);
   if (fields[5] != IGN && cal.get(Calendar.SECOND) != fields[5])
     result.add("second " + cal.get(Calendar.SECOND) + " != " + fields[5]);
   return result;
 }