public static String castToString(DateTime dt, int format) {
    if (format == DateTimeFormat_W3_dateTime) /* Format as schema dateTime */ {
      return dt.toString();
    }
    if (format == DateTimeFormat_W3_date) /* Format as schema date */ {
      return dt.toDateString(true);
    }
    if (format == DateTimeFormat_W3_time) /* Format as schema time */ {
      return dt.toTimeString();
    }
    if (format == DateTimeFormat_W3_gYear) /* Format as schema gYear */ {
      String result = "";
      int year = dt.getYear();
      if (year < 0) {
        result += '-';
        year = -year;
      }

      result += formatNumber(year, 4);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result = formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format == DateTimeFormat_W3_gYearMonth) /* Format as schema gYearMonth */ {
      String result = "";
      int year = dt.getYear();
      int month = dt.getMonth();
      if (year < 0) {
        result += '-';
        year = -year;
      }

      result += formatNumber(year, 4);
      result += '-';
      result += formatNumber(month, 2);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result += formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format == DateTimeFormat_W3_gMonth) /* Format as schema gMonth */ {
      String result = "--";
      int month = dt.getMonth();
      result += formatNumber(month, 2);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING) formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format == DateTimeFormat_W3_gMonthDay) /* Format as schema gMonthDay */ {
      String result = "--";
      int month = dt.getMonth();
      int day = dt.getDay();
      result += formatNumber(month, 2);
      result += '-';
      result += formatNumber(day, 2);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result += formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format == DateTimeFormat_W3_gDay) /* Format as schema gDay */ {
      String result = "---";
      int day = dt.getDay();
      result += formatNumber(day, 2);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result += formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format
        == DateTimeFormat_S_DateTime) /* Format as standard DateTime "YYYY-MM-DD HH:MM:SS" */ {
      StringBuffer s = new StringBuffer();
      s.append(dt.toDateString());
      s.append(" ");
      s.append(dt.toTimeString());
      return s.toString();
    }
    if (format == DateTimeFormat_S_Seconds) /* Format as number of seconds since epoch */ {
      String result = "";
      long milliseconds = dt.getTimeValue();
      if (milliseconds < 0) {
        result += '-';
        milliseconds = -milliseconds;
      }
      result += formatNumber(milliseconds / 1000, 1);
      result += formatFraction(milliseconds % 1000, 7);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result += formatTimezone(dt.getTimezoneOffset());
      return result;
    }
    if (format == DateTimeFormat_S_Days) /* Format as number of days since epoch */ {
      String result = "";
      long milliseconds = dt.getTimeValue();
      if (milliseconds < 0) {
        result += '-';
        milliseconds = -milliseconds;
      }
      result += formatNumber(milliseconds / (86400 * 1000), 1);
      result += formatFraction(milliseconds % (86400 * 1000), 7);
      if (dt.hasTimezone() != CalendarBase.TZ_MISSING)
        result += formatTimezone(dt.getTimezoneOffset());
      return result;
    }

    throw new IllegalArgumentException("Unsupported datetime format.");
  }
 public static BigInteger GYearToInteger(DateTime dt) {
   return BigInteger.valueOf((long) dt.getYear());
 }
 public static String castToString(DateTime dt) {
   return dt.toString();
 }
 public static DateTime castToDateTime(String s) {
   if (s == null) throw new java.lang.NullPointerException();
   if (s.length() == 0) throw new IllegalArgumentException("Cast to DateTime failed.");
   return DateTime.parse(s);
 }