Java Date to Time getXsdDateTime(Date time)

Here you can find the source of getXsdDateTime(Date time)

Description

get Xsd Date Time

License

Apache License

Declaration

public static String getXsdDateTime(Date time) 

Method Source Code

//package com.java2s;
/*/*w ww  . j  a v  a  2 s.com*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

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

public class Main {
    public static String getXsdDateTime(Date time) {
        Date now = time;
        DateFormat ISO8601Local = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        TimeZone timeZone = TimeZone.getDefault();
        ISO8601Local.setTimeZone(timeZone);
        int offset = timeZone.getOffset(now.getTime());
        String sign = "+";
        if (offset < 0) {
            offset = -offset;
            sign = "-";
        }
        int hours = offset / 3600000;
        int minutes = (offset - hours * 3600000) / 60000;
        if (offset != hours * 3600000 + minutes * 60000) {
            // E.g. TZ=Asia/Riyadh87
            throw new RuntimeException(
                    "TimeZone offset (" + sign + offset + " ms) is not an exact number of minutes");
        }
        DecimalFormat twoDigits = new DecimalFormat("00");
        String ISO8601Now = ISO8601Local.format(now) + sign + twoDigits.format(hours) + ":"
                + twoDigits.format(minutes);
        return ISO8601Now;
    }
}

Related

  1. getTimeString(Date date)
  2. getTimeString(Date self)
  3. getTimeString(java.util.Date date)
  4. getTimeText(Date date)
  5. getXSDDateTime()
  6. getXsdDateTime(final Date d)
  7. removeTime(Date date)
  8. serializeDateTime(final Date date)
  9. serializeXsdDateTime(Date date)