Java Time Format getXSDFormatter(DateTime date)

Here you can find the source of getXSDFormatter(DateTime date)

Description

get XSD Formatter

License

Open Source License

Declaration

public static DateTimeFormatter getXSDFormatter(DateTime date) 

Method Source Code

//package com.java2s;
/**/*  ww w  . j a  va 2  s  .  c  o  m*/
 * Copyright (C) 2010 MediaShelf <http://www.yourmediashelf.com/>
 *
 * This file is part of fedora-client.
 *
 * fedora-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fedora-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with fedora-client.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.DecimalFormat;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.joda.time.DateTime;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

public class Main {
    private final static ConcurrentMap<String, DateTimeFormatter> formatters = new ConcurrentHashMap<String, DateTimeFormatter>();

    public static DateTimeFormatter getXSDFormatter(DateTime date) {
        int len = 0;
        int millis = date.getMillisOfSecond();

        if (millis > 0) {
            // 0.050 becomes .05 (up to three digits, dropping trailing 0s)
            DecimalFormat df = new DecimalFormat(".###");
            double d = millis / 1000.0;
            len = String.valueOf(df.format(d)).length() - 1;
        }
        return getXSDFormatter(len);
    }

    /**
     * Returns an xsd:dateTime formatter with the specified millisecond precision.
     *
     * @param millisLength number of digits of millisecond precision. Currently,
     * only 0-3 are valid arguments.
     * @return a formatter for yyyy-MM-dd'T'HH:mm:ss[.SSS]Z
     */
    public static DateTimeFormatter getXSDFormatter(int millisLength) {
        String key = String.valueOf(millisLength);
        if (formatters.get(key) == null) {
            DateTimeFormatterBuilder bldr = new DateTimeFormatterBuilder().appendYear(4, 9).appendLiteral('-')
                    .appendMonthOfYear(2).appendLiteral('-').appendDayOfMonth(2).appendLiteral('T')
                    .appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':')
                    .appendSecondOfMinute(2);
            if (millisLength > 0) {
                bldr = bldr.appendLiteral('.').appendFractionOfSecond(millisLength, millisLength);
            }
            bldr = bldr.appendTimeZoneOffset("Z", true, 2, 4);
            formatters.putIfAbsent(key, bldr.toFormatter());
        }
        return formatters.get(key);
    }
}

Related

  1. getTimeStringFormatedForSWANWithDelim( final Object pObjTime, final String pStrDelim)
  2. getTimeSystem(String formatTime)
  3. getTwoTimeInterval(String startTime, String endTime, String format)
  4. getUniversalDateTimeFormat()
  5. getXsdDateTimeFormat()
  6. isDateFormat(String timeString)
  7. long2Datestr(long time, String format)
  8. long2String(long longTime, String dataFormat)
  9. long2String(long longTime, String dataFormat)