0

I want to convert a string that contains a date to a GregorianCalendar in the form "dd.mm.yyyy". I have used the below code. I am able to convert to the desired datatype, but not in the desired format. Any suggestions regarding this would be helpful to me.

public class StringToCalander {
    public static void main(String args[]) throws DatatypeConfigurationException {
        String date="20160916";     
        Date dob=null;      
        DateFormat df=new SimpleDateFormat("yyyyMMdd");
        try {
            dob=df.parse( date );
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(dob);
        XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        System.out.println(" xml date value is:"+xmlDate);
        //output is 2016-09-16T00:00:00.000+02:00
        //but i need output in the format dd.mm.yyyy(16.09.2016)                
    }
}
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
siva
  • 5
  • 1
  • 6

2 Answers2

0

Try this. (updated for GregorianCalendar as well)

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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

        String FORMATER = "ddMMyyyy";

        DateFormat format = new SimpleDateFormat(FORMATER);

        Date date2 = new Date();
        XMLGregorianCalendar gDateFormatted = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(format.format(date2));
        System.out.println("xmlDate via GregorianCalendar: " + gDateFormatted);

    }
}
mhasan
  • 3,703
  • 1
  • 18
  • 37
  • I need the desired output (dd.mm.yyyy) to be stored in a calendar type variable not a string . XMLGregorianCalendar xmlDate=?(desired output); – siva Sep 19 '16 at 09:51
  • the "dd.MM.yyyy" doesnt seem to be valid Lexical representation accepted by XMLGregorianCalendarImpl , I tried and could get the format as "ddMMyyyy". I have updated my answer with the same. Also attached is the schema details, you may validate from the same. https://www.w3.org/TR/xmlschema-2/#dt-lexical-space – mhasan Sep 19 '16 at 10:55
0

You can use a Date-Object to format your XMLGregorianCalendar:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

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 {

    String format = "dd'.'MM'.'yyyy";

    DateFormat formatter = new SimpleDateFormat(format);

    GregorianCalendar date = new GregorianCalendar();
    XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance()
            .newXMLGregorianCalendar(date);

    Date dateObject = xmlDate.toGregorianCalendar().getTime();
    System.out.println("xml date value is: " + formatter.format(dateObject));

    }
}