问题
I want to use a Date in XMLGregorianCalendar
format for sending to a web service. The web service expects information in yyyy-dd-mm
format. I use the below code to create an XMLGregorianCalendar
and send it to web service.
Date dob = null;
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try {
XMLGregorianCalendar date2;
dob = df.parse("13/06/1983");
GregorianCalendar c = new GregorianCalendar();
c.setTimeInMillis(dob.getTime());
date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
System.out.println(date2);
}
catch(DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Unfortunately I always get the date as 1983-06-13T00:00:00.000-04:00
. Time is also getting included in the output. Is it possible to get only the date? Could you please help me?
回答1:
you don't need to specify a "SimpleDateFormat", it's simple: You must do specify the constant "DatatypeConstants.FIELD_UNDEFINED" where you don't want to show
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
回答2:
Yeah Got it...
Date dob=null;
DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
dob=df.parse( "13/06/1983" );
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(dob);
XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
This will give it in correct format.
回答3:
Much simpler using only SimpleDateFormat, without passing all the parameters individual:
String FORMATER = "yyyy-MM-dd'T'HH:mm:ss'Z'";
DateFormat format = new SimpleDateFormat(FORMATER);
Date date = new Date();
XMLGregorianCalendar gDateFormatted =
DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(date));
Full example here.
Note: This is working only to remove the last 2 fields: milliseconds and timezone or to remove the entire time component using formatter yyyy-MM-dd
.
回答4:
There isn’t really an ideal conversion, but I would like to supply a couple of options.
java.time
First, you should use LocalDate
from java.time, the modern Java date and time API, for parsing and holding your date. Avoid Date
and SimpleDateFormat
since they have design problems and also are long outdated. The latter in particular is notoriously troublesome.
DateTimeFormatter originalDateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
String dateString = "13/06/1983";
LocalDate date = LocalDate.parse(dateString, originalDateFormatter);
System.out.println(date);
The output is:
1983-06-13
Do you need to go any further? LocalDate.toString()
produces the format you asked about.
Format and parse
Assuming that you do require an XMLGregorianCalendar
the first and easy option for converting is:
XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(date.toString());
System.out.println(xmlDate);
1983-06-13
Formatting to a string and parsing it back feels like a waste to me, but as I said, it’s easy and I don’t think that there are any surprises about the result being as expected.
Pass year, month and day of month individually
XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance()
.newXMLGregorianCalendarDate(date.getYear(), date.getMonthValue(),
date.getDayOfMonth(), DatatypeConstants.FIELD_UNDEFINED);
The result is the same as before. We need to make explicit that we don’t want a time zone offset (this is what DatatypeConstants.FIELD_UNDEFINED
specifies). In case someone is wondering, both LocalDate
and XMLGregorianCalendar
number months the way humans do, so there is no adding or subtracting 1.
Convert through GregorianCalendar
I only show you this option because I somehow consider it the official way: convert LocalDate
to ZonedDateTime
, then to GregorianCalendar
and finally to XMLGregorianCalendar
.
ZonedDateTime dateTime = date.atStartOfDay(ZoneOffset.UTC);
GregorianCalendar gregCal = GregorianCalendar.from(dateTime);
XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(gregCal);
xmlDate.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
xmlDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
I like the conversion itself since we neither need to use strings nor need to pass individual fields (with care to do it in the right order). What I don’t like is that we have to pass a time of day and a time zone offset and then wipe out those fields manually afterwards.
回答5:
This is an easy way for any format. Just change it to required format string
XMLGregorianCalendar gregFmt = DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
System.out.println(gregFmt);
来源:https://stackoverflow.com/questions/14060161/specify-the-date-format-in-xmlgregoriancalendar