Java XML Date Create xmlToDate(XMLGregorianCalendar calendar, DateFormat format)

Here you can find the source of xmlToDate(XMLGregorianCalendar calendar, DateFormat format)

Description

if(null == calendar) return ""; if(null == format) default use format_yyyyMMdd_HHmmss

License

Open Source License

Declaration

public static String xmlToDate(XMLGregorianCalendar calendar, DateFormat format) 

Method Source Code


//package com.java2s;

import java.sql.Timestamp;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    public final static DateFormat format_yyyyMMdd_HHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**//from  w w w  .  j  ava2 s. c  o m
     * if(null == calendar) return ""; if(null == format) default use format_yyyyMMdd_HHmmss
     */
    public static String xmlToDate(XMLGregorianCalendar calendar, DateFormat format) {
        if (null == calendar) {
            return "";
        }
        if (null == format) {
            format = format_yyyyMMdd_HHmmss;
        }

        return Format(format, calendar.toGregorianCalendar().getTime());
    }

    public static String Format(DateFormat format, Timestamp time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time);
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }

    public static String Format(DateFormat format, Date time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time).replace("24:", "00:");
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }

    public static String Format(DateFormat format, long time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time);
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }
}

Related

  1. xmlGregorianCalendarToDate(XMLGregorianCalendar cal)
  2. xmlGregorianCalendarToDate(XMLGregorianCalendar calendar)
  3. XMLGregorianCalendarToDate(XMLGregorianCalendar gc)
  4. xmlGregorianCalendarToTimestamp(XMLGregorianCalendar xgc)
  5. xmlGregorianToString(XMLGregorianCalendar timestamp)
  6. xmlToDateTime(final XMLGregorianCalendar xmlCal)