Java UTC Date SerializeUtc(Date dt)

Here you can find the source of SerializeUtc(Date dt)

Description

Serialize Utc

License

Open Source License

Declaration

public static String SerializeUtc(Date dt) 

Method Source Code

//package com.java2s;
/*//www  .  j  av  a 2s.  com
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
    
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  This program 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 General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

import java.util.*;

public class Main {
    public final static TimeZone UTC = TimeZone.getTimeZone("UTC");

    public static String SerializeUtc(Date dt) {
        Calendar c = new GregorianCalendar(UTC, Locale.US);
        c.setTime(dt);
        StringBuilder b = new StringBuilder(21);
        b.append(pad0s(c.get(Calendar.YEAR), 4));
        b.append("-");
        b.append(pad0s(c.get(Calendar.MONTH) + 1, 2));
        b.append("-");
        b.append(pad0s(c.get(Calendar.DAY_OF_MONTH), 2));
        b.append("T");
        b.append(pad0s(c.get(Calendar.HOUR_OF_DAY), 2));
        b.append(":");
        b.append(pad0s(c.get(Calendar.MINUTE), 2));
        b.append(":");
        b.append(pad0s(c.get(Calendar.SECOND), 2));
        b.append("Z");
        return b.toString();
    }

    public static String pad0s(int i, int width) {
        StringBuilder s = new StringBuilder(width);
        s.append(String.valueOf(i));
        while (s.length() < width)
            s.insert(0, "0");
        return s.toString();
    }
}

Related

  1. getUtcTimestampAsString(Date date)
  2. getUTCTimeString4Digits(Date date)
  3. nowInUTC()
  4. nowUtc(String pattern)
  5. nowUTCString()
  6. stringToUTC(String utc)
  7. toUTCDate(String dateStr)
  8. toUTCString(Date date)
  9. toUTCString(long t)