Java xmlgregoriancalendar to string

Java xmlgregoriancalendar to string

All Implemented Interfaces: Cloneable, Serializable public class XMLGregorianCalendar extends Object implements Serializable, Cloneable

Representation for W3C XML Schema 1.0 date/time datatypes. Specifically, these date/time datatypes are dateTime , time , date , gYearMonth , gMonthDay , gYear gMonth and gDay defined in the XML Namespace «http://www.w3.org/2001/XMLSchema» . These datatypes are normatively defined in W3C XML Schema 1.0 Part 2, Section 3.2.7-14.

The table below defines the mapping between XML Schema 1.0 date/time datatype fields and this class’ fields. It also summarizes the value constraints for the date and time fields defined in W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats.

All maximum value space constraints listed for the fields in the table above are checked by factory methods, setter methods and parse methods of this class. IllegalArgumentException is thrown when parameter’s value is outside the maximum value constraint for the field. Validation checks, for example, whether days in month should be limited to 29, 30 or 31 days, that are dependent on the values of other fields are not checked by these methods.

  • factory methods to create instances
  • accessors/mutators for independent date/time fields
  • conversion between this class and W3C XML Schema 1.0 lexical representation
  • conversion between this class and java.util.GregorianCalendar
  • partial order relation comparator method, compare(XMLGregorianCalendar, XMLGregorianCalendar)
  • equals(Object) defined relative to compare(XMLGregorianCalendar, XMLGregorianCalendar) .
  • addition operation with Duration . instance as defined in W3C XML Schema 1.0 Part 2, Appendix E, Adding durations to dateTimes.

Field Summary
static int APRIL
Value for fourth month of year.
static int AUGUST
Value for eighth month of year.
static QName DATE
Fully qualified name for W3C XML Schema 1.0 datatype date .
static QName DATETIME
Fully qualified name for W3C XML Schema 1.0 datatype dateTime .
static int DECEMBER
Value for twelve month of year.
static int EQUAL
Partial order relation comparison result.
static int FEBRUARY
Value for second month of year.
static int FIELD_UNDEFINED
Designation that an «int» field is not set.
static QName GDAY
Fully qualified name for W3C XML Schema 1.0 datatype gDay .
static QName GMONTH
Fully qualified name for W3C XML Schema 1.0 datatype gMonth .
static QName GMONTHDAY
Fully qualified name for W3C XML Schema 1.0 datatype gMonthDay .
static int GREATER
Partial order relation comparison result.
static QName GYEAR
Fully qualified name for W3C XML Schema 1.0 datatype gYear .
static QName GYEARMONTH
Fully qualified name for W3C XML Schema 1.0 datatype gYearMonth .
static int INDETERMINATE
Partial order relation comparison result.
static int JANUARY
Value for first month of year.
static int JULY
Value for seventh month of year.
static int JUNE
Value for sixth month of year.
static XMLGregorianCalendar LEAP_YEAR_DEFAULT
Use as a template for default field values when converting to a java.util.GregorianCalendar , set to a leap year date of January 1, 0400 at midnight.
static int LESSER
Partial order relation comparison result.
static int MARCH
Value for third month of year.
static int MAX_TIMEZONE_OFFSET
W3C XML Schema max timezone offset is -14:00.
static int MAY
Value for fifth month of year.
static int MIN_TIMEZONE_OFFSET
W3C XML Schema min timezone offset is +14:00.
static int NOVEMBER
Value for eleven month of year.
static int OCTOBER
Value for tenth month of year.
static int SEPTEMBER
Value for ninth month of year.
static QName TIME
Fully qualified name for W3C XML Schema 1.0 datatype time .
Читайте также:  Append string to string in javascript
Constructor Summary
XMLGregorianCalendar ()
Create an instance with all date/time datatype fields set to FIELD_UNDEFINED or null respectively.
Method Summary
void add (Duration duration)
Add duration to this instance.
void clear ()
Unset all fields to undefined.
Object clone ()
Creates and returns a copy of this object.
static int compare (XMLGregorianCalendar lhs, XMLGregorianCalendar rhs)
Compare two instances of W3C XML Schema 1.0 date/time datatypes according to partial order relation defined in W3C XML Schema 1.0 Part 2, Section 3.2.7.3, Order relation on dateTime.
static XMLGregorianCalendar createDate (int year, int month, int day, int timezone)
Create a Java representation of XML Schema builtin datatype date or g* .
static XMLGregorianCalendar createDateTime (BigInteger year, int month, int day, int hours, int minutes, int seconds, BigDecimal fractionalSecond, int timezone)
Create a Java representation of XML Schema builtin datatype dateTime .
static XMLGregorianCalendar createDateTime (int year, int month, int day, int hour, int minute, int second)
Create a Java instance of XML Schema builtin datatype dateTime.
static XMLGregorianCalendar createDateTime (int year, int month, int day, int hours, int minutes, int seconds, int milliseconds, int timezone)
Create a Java representation of XML Schema builtin datatype dateTime .
static XMLGregorianCalendar createTime (int hours, int minutes, int seconds, BigDecimal fractionalSecond, int timezone)
Create a Java instance of XML Schema builtin datatype time.
static XMLGregorianCalendar createTime (int hours, int minutes, int seconds, int timezone)
Create a Java instance of XML Schema builtin datatype time .
static XMLGregorianCalendar createTime (int hours, int minutes, int seconds, int milliseconds, int timezone)
Create a Java instance of XML Schema builtin datatype time.
boolean equals (Object obj)
Indicates whether parameter obj is «equal to» this one.
static XMLGregorianCalendar fromGregorianCalendar (GregorianCalendar cal)
Convert a java.util.GregorianCalendar to XML Schema 1.0 representation.
int getDay ()
Return day in month or FIELD_UNDEFINED .
BigInteger getEon ()
Get the eons of the year.
BigInteger getEonAndYear ()
Return XML Schema 1.0 dateTime datatype field for year .
BigDecimal getFractionalSecond ()
Return fractional seconds.
int getHour ()
Return hours or FIELD_UNDEFINED .
int getMillisecond ()
Return milliseconds or FIELD_UNDEFINED .
int getMinute ()
Return minutes or FIELD_UNDEFINED . Returns FIELD_UNDEFINED if this field is not defined.
int getMonth ()
Return number of month or FIELD_UNDEFINED .
int getSecond ()
Return seconds or FIELD_UNDEFINED .
int getTimezone ()
Return timezone offset in minutes or FIELD_UNDEFINED if this optional field is not defined.
QName getXMLSchemaType ()
Return the name of the XML Schema date/time type that this instance maps to.
int getYear ()
Return low order component XML Schema 1.0 dataTime datatype field for year or FIELD_UNDEFINED .
int hashCode ()
Returns a hash code consistent with the definition of the equals method.
boolean isValid ()
Validate instance by getXMLSchemaType() constraints.
static XMLGregorianCalendar parse (String lexicalRepresentation)
Constructs a new XMLGregorianCalendar object by parsing its lexical string representation as defined in XML Schema 1.0 Part 2, Section 3.2.13.1, Lexical Representation.
void setDay (int day)
Set days in month.
void setMonth (int month)
Set month.
void setTime (int hour, int minute, int second)
Set time as one unit.
void setTime (int hour, int minute, int second, BigDecimal fractional)
Set time as one unit, including the optional infinite precison fractional seconds.
void setTime (int hour, int minute, int second, int millisecond)
Set time as one unit, including optional milliseconds.
void setTimezone (int offset)
Set the number of minutes in the timezone offset.
void setYear (BigInteger year)
Set low and high order component of XSD dateTime year field.
void setYear (int year)
Set year of XSD dateTime year field.
GregorianCalendar toGregorianCalendar ()
Convert this to java.util.GregorianCalendar .
GregorianCalendar toGregorianCalendar (TimeZone timezone, Locale aLocale, XMLGregorianCalendar defaults)
Convert this along with provided parameters to java.util.GregorianCalendar instance.
String toXMLFormat ()
Return the lexical representation of this instance.
Читайте также:  Readonly typescript что это
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait

JANUARY

public static final int JANUARY

FEBRUARY

public static final int FEBRUARY

Источник

Java XMLGregorianCalendar

Learn to convert XMLGregorianCalendar to Date class and format to string. Also, learn to apply timezone changes and daylight saving effects as well. This class is a representation of the W3C XML Schema 1.0 date/time datatypes that defines clear rules for specifying dates in XML format.

1. Creating XMLGregorianCalendar

Using the constructor of XMLGregorianCalendar is not recommended and docs suggest using the DataTypeFactory to create its instance.

//Create XMLGregorianCalendar GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date()); XMLGregorianCalendar xCal = DatatypeFactory.newInstance() .newXMLGregorianCalendar(cal);

2. Converting XMLGregorianCalendar to Date

The GregorianCalendar.getTime() method returns the java.util.Date object.

XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal.toGregorianCalendar().getTime(); System.out.println(date);
Thu Feb 17 23:29:25 IST 2022

3. Formatting XMLGregorianCalendar

The simplest way to format XMLGregorianCalendar is to first convert it to the Date object, and format the Date to String.

XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal.toGregorianCalendar().getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); String formattedString = df.format(date); System.out.println(formattedString);

4. Formatting in Another Timezone

If you want to print the formatted date in another timezone, such as GMT , then we will need to set timezone is DateFormat instance.

XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal.toGregorianCalendar().getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String formattedString = df.format(date); System.out.println(formattedString);

5. Handling Daylight Saving Time (DST) Effect

To check whether current time or adjusted time falls under DST (daylight saving time), then you might want to handle those changes as well.

Читайте также:  Php string length functions

This will be useful when we are converting a Date from one timezone to another timezone, and there may be DST effects on those timezones.

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; public class Main < public static void main(String[] args) throws DatatypeConfigurationException < GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date()); XMLGregorianCalendar xCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); System.out.println(convertXmlGregorianToString(xCal)); >public static String convertXmlGregorianToString(XMLGregorianCalendar xc) < TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); TimeZone fromTimeZone = TimeZone.getDefault(); GregorianCalendar gCalendar = xc.toGregorianCalendar(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); Date date = adjustToTimezone(gCalendar.getTime(), fromTimeZone, gmtTimeZone); String dateString = df.format(date); return dateString; >public static Date adjustToTimezone(Date date, TimeZone fromZone, TimeZone toZone) < Date adjustedToTimezone = new Date(date.getTime() + toZone.getRawOffset() - fromZone.getRawOffset()); // Is the adjusted date in Daylight savings? if (fromZone.inDaylightTime(adjustedToTimezone) != toZone.inDaylightTime(adjustedToTimezone)) < adjustedToTimezone = new Date(adjustedToTimezone.getTime() + toZone.getDSTSavings() - fromZone.getDSTSavings()); >return adjustedToTimezone; > >

Источник

Оцените статью