Java Date to String dateToUtcString(Date date)

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

Description

Transform given Date into String using pattern yyyy-MM-dd'T'HH:mm:ss'Z'.

License

Open Source License

Parameter

Parameter Description
date date to format

Return

String

Declaration

public static String dateToUtcString(Date date) 

Method Source Code

//package com.java2s;
/*//from  ww w  .jav a2s  . c o  m
 * Copyright (C) 2016 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2016 Brocade Communications Systems, Inc. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static final TimeZone TZ_UTC = TimeZone.getTimeZone("UTC");
    private static final String[] DATE_AND_TIME_FORMATS = { "yyyy-MM-dd'T'HH:mm:ss'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" };

    /**
     * Transform given {@link Date} into {@link String} using pattern
     * yyyy-MM-dd'T'HH:mm:ss'Z'.
     *
     * @param date
     *            date to format
     * @return String
     */
    public static String dateToUtcString(Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat(DATE_AND_TIME_FORMATS[0]);
        sdf.setTimeZone(TZ_UTC);
        return sdf.format(date);
    }
}

Related

  1. dateToStringWithPattern(Date date, String pattern)
  2. dateToStringWithPattern(Date date, String pattern)
  3. dateToStringWithTime(Date date)
  4. dateToTimeString(Date date)
  5. dateToUTCDate(Date d)
  6. dateToW3c(Date d)
  7. dateToW3CDTF(Date date)
  8. dateToW3CDTF(Date date)
  9. dateToyyyyMMddHHmmss(Date date)