Java tutorial
//package com.java2s; /* * Geotoolkit.org - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2009-2012, Open Source Geospatial Foundation (OSGeo) * (C) 2009-2012, Geomatys * * This library 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; * version 2.1 of the License. * * This library 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. */ import java.util.Date; import java.util.Locale; import java.util.TimeZone; import java.util.Calendar; import java.util.GregorianCalendar; import javax.xml.bind.DatatypeConverter; public class Main { /** * The Gregorian calendar to use for {@link #printDateTime}. * * @since 3.06 */ private static final ThreadLocal<Calendar> CALENDAR = new ThreadLocal<Calendar>() { @Override protected Calendar initialValue() { return new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.CANADA); } }; /** * Formats a date value in a string, assuming UTC timezone and Canada locale. * This method should be used only for occasional formatting. * * @param date The date to format, or {@code null}. * @return The formatted date, or {@code null} if the given date was null. * * @see DatatypeConverter#printDateTime(Calendar) * * @since 3.06 */ public static String printDateTime(final Date date) { if (date == null) { return null; } final Calendar calendar = CALENDAR.get(); calendar.setTime(date); return DatatypeConverter.printDateTime(calendar); } }