如何从XmlGregorianCalendar获取日期和时间

时间:2014-07-17 10:11:18

标签: java

我有这个日期的代码:

public static XMLGregorianCalendar retrieveXMLGregorianCalendar(Date source)  {
            XMLGregorianCalendar dest = null;
            Calendar calendar = Calendar.getInstance();
            if (source !=null) {
                calendar.setTime(source);
                DatatypeFactory datatypeFactory;
                try {
                    datatypeFactory = DatatypeFactory.newInstance();
                    dest =  datatypeFactory.newXMLGregorianCalendarDate(calendar.get(Calendar.YEAR), calendar
                            .get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
                    dest.setHour(calendar.get(Calendar.HOUR));
                    dest.setMinute(calendar.get(Calendar.MINUTE));
                    dest.setSecond(calendar.get(Calendar.SECOND));
                } catch (DatatypeConfigurationException e) {
                    e.printStackTrace();
                    throw new SoapFaultException("Date Trasfomation Exception");
                }
            }
            return dest;
        }

和小时的代码:

public static XMLGregorianCalendar retrieveXMLGregorianCalendarTime(Date source)  {
        XMLGregorianCalendar dest = null;
        Calendar calendar = Calendar.getInstance();
        if (source !=null) {
          calendar.setTime(source);
          DatatypeFactory datatypeFactory;
          try {
            datatypeFactory = DatatypeFactory.newInstance();
            dest =  datatypeFactory.newXMLGregorianCalendarTime(calendar.get(Calendar.HOUR_OF_DAY), calendar
                .get(Calendar.MINUTE) + 1, calendar.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED);
            dest.setYear(calendar.get(Calendar.YEAR));
            dest.setMonth(calendar.get(Calendar.MONTH) + 1);
            dest.setDay(calendar.get(Calendar.DAY_OF_MONTH));
          } catch (DatatypeConfigurationException e) {
            e.printStackTrace();
            return null;
          }
        }
        return dest;
      }

我只是想像这样的格式(dd / MM / YYYY)和像这样的小时(hh:mm:ss)来捕捉日期。但我不知道如何抓住它。我试过几次,试图不赶上约会的时间或分钟,但我的日食说没有设置分钟或小时。

2 个答案:

答案 0 :(得分:0)

格式化日期(到字符串):

SimpleDateFormat sdfDate = new SimpleDateFormat("dd.MM.yyyy");

SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");

SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

System.out.println(sdfDate.format(source));

System.out.println(sdfTime.format(source));

System.out.println(sdfDateTime.format(source));

答案 1 :(得分:0)

该方法会返回xmlGregorianCalendar

将其转换为GregorianCalendar

最后使用GregorianCalendargetTime()获取时间并使用SimpleDateFormat中的dd/MM/YYYY HH:mm:ss格式化

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);
XMLGregorianCalendar xmlGregorianCalendar = retrieveXMLGregorianCalendar(new Date());
Date date = xmlGregorianCalendar.toGregorianCalendar().getTime();
System.out.println(dateFormat.format(date));