将SQL Timestamp中的日期时间与XMLGregorianCalendar进行比较,并忽略JAVA中的秒数

时间:2015-05-26 08:27:50

标签: java

我尝试将SQL Timestamp转换为XMLGregorianCalendar,然后与其他XMLGregorianCalendar进行比较,但问题是如果我在转换时将DatatypeConstants.FIELD_UNDEFINED设置为秒和毫秒“为XMLGregorianCalendar设置的字段集无效”

请提出任何建议 提前致谢

2 个答案:

答案 0 :(得分:0)

如果没有更多信息,您可以通过几种方式进行检查。

1)将两个日期转换为日历实例并将时间清零然后只需运行compareto

2)获取日历实例并手动检查年份,年日,最后是月到月

public class DateCheck
{

  public static void main(String[] args)
  {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(new Date());
    try
    {
      XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
      Timestamp timeStamp = new Timestamp(System.currentTimeMillis());

      System.out.println("Are dates equal using manual check : " + manualDateCheck(timeStamp, xmlGregorianCalendar));

      Calendar timeStampCalendar = Calendar.getInstance();
      timeStampCalendar.setTime(timeStamp);
      System.out.println("Are dates equal using calendar check : " + calendarCheck(timeStampCalendar, xmlGregorianCalendar.toGregorianCalendar()));

    }
    catch (DatatypeConfigurationException e)
    {
      e.printStackTrace();
    }
  }


  public static boolean manualDateCheck(Timestamp timestamp, XMLGregorianCalendar xmlGregorianCalendar)
  {
    Calendar cal = xmlGregorianCalendar.toGregorianCalendar();
    Calendar otherCal = Calendar.getInstance();
    otherCal.setTime(timestamp);
    if (cal.get(Calendar.YEAR) != otherCal.get(Calendar.YEAR))
    {
      return false;
    }
    if (cal.get(Calendar.MONTH) != otherCal.get(Calendar.MONTH))
    {
      return false;
    }
    if (cal.get(Calendar.DATE) != otherCal.get(Calendar.DATE))
    {
      return false;
    }
    if (cal.get(Calendar.HOUR) != otherCal.get(Calendar.HOUR))
    {
      return false;
    }
    if (cal.get(Calendar.MINUTE) != otherCal.get(Calendar.MINUTE))
    {
      return false;
    }
    return true;
  }

  public static boolean calendarCheck(Calendar calendar1, Calendar calendar2)
  {
    return zeroCalendar(calendar1).compareTo(zeroCalendar(calendar2)) == 0;
  }

  public static Calendar zeroCalendar(Calendar calendar)
  {
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar;
  }
}

答案 1 :(得分:-1)

谢谢Kenneth, calenderCheck更简单,我将使用.. :)但我尝试了许多方式,我做了以下其他方式。

如果下面没有,请纠正我,

public class TestMain {

  public static void main(String[] args) throws DatatypeConfigurationException{

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(new Date());
    XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
    String sqlTimestamp = dateFormat.format(timeStamp);

    Timestamp xmlToSqlTimestamp = new Timestamp(xmlGregorianCalendar.toGregorianCalendar().getTimeInMillis());
    String xmlTimestamp = dateFormat.format(xmlToSqlTimestamp);
    System.out.println(timeStamp+" : "+xmlGregorianCalendar);
    System.out.println(sqlTimestamp+" : "+xmlTimestamp);

    System.out.println("Time stamps are equal : "+sqlTimestamp.equalsIgnoreCase(xmlTimestamp));
  }
}