Java Date to String convertStringoXSDString(Date date)

Here you can find the source of convertStringoXSDString(Date date)

Description

convert Stringo XSD String

License

Apache License

Declaration

public static String convertStringoXSDString(Date date) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final Date ONE_BCE = new Date(-62167392000000L);
    private static final Date ONE_CE = new Date(-62135769600000L);

    public static String convertStringoXSDString(Date date) {
        if (date == null) {
            return null;
        }//w  ww .ja v a2s .c  o  m
        StringBuilder lexicalForm;
        String dateTime = convertDateToString(date, false);
        int len = dateTime.length() - 1;
        if (dateTime.indexOf('.', len - 4) != -1) {
            while (dateTime.charAt(len - 1) == '0') {
                len--; // fractional seconds may not with '0'.
            }
            if (dateTime.charAt(len - 1) == '.') {
                len--;
            }
            lexicalForm = new StringBuilder(dateTime.substring(0, len));
            lexicalForm.append('Z');
        } else {
            lexicalForm = new StringBuilder(dateTime);
        }

        if (date.before(ONE_CE)) {
            DateFormat df = new SimpleDateFormat("yyyy");
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            StringBuilder year = new StringBuilder(String.valueOf(Integer.parseInt(df.format(date)) - 1));
            while (year.length() < 4) {
                year.insert(0, '0');
            }
            lexicalForm.replace(0, lexicalForm.indexOf("-", 4), year.toString());
            if (date.before(ONE_BCE)) {
                lexicalForm.insert(0, "-");
            }
        }
        return lexicalForm.toString();
    }

    public static String convertDateToString(Date date, boolean millis) {
        if (date == null) {
            return null;
        } else {
            DateFormat df;
            if (millis) {
                df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            } else {
                df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            }
            df.setTimeZone(TimeZone.getTimeZone("GMT"));

            if (date.before(ONE_CE)) {
                StringBuilder sb = new StringBuilder(df.format(date));
                sb.insert(0, "-");
                return sb.toString();
            } else {
                return df.format(date);
            }
        }
    }
}

Related

  1. convertDateToString(final Date datum, final boolean withDay)
  2. convertDateToString(java.util.Date dt, String pattern)
  3. convertDateToString(String dateFormat, Date date)
  4. convertDateToString(String mask, Date date)
  5. convertDateToURLFormat(final String dateToBeConvert)
  6. convertTimeToString(Date date, String pattern)
  7. convertToString(Date date)
  8. ConvertToString(Date date, String format)
  9. convertToString(Date date, String style)