Java tutorial
//package com.java2s; /** * This file is part of the au-xml-util package * * Copyright Trenton D. Adams <trenton daught d daught adams at gmail daught ca> * * au-xml-util 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. * * au-xml-util 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 au-xml-util. If not, see <http://www.gnu.org/licenses/>. * * See the COPYING file for more information. */ import java.text.SimpleDateFormat; import java.util.*; public class Main { /** * Formats a date in the xml xs:dateTime format, for mountain time. This * method does not support any other timezone. * * @param calendar the calendar to format * * @return the formatted date */ public static String xsDateTimeFormatMountain(final Calendar calendar) { final SimpleDateFormat dateFormat; final SimpleDateFormat timeFormat; final TimeZone currentTimeZone; dateFormat = new SimpleDateFormat("yyyy-MM-dd"); timeFormat = new SimpleDateFormat("HH:mm:ss"); currentTimeZone = TimeZone.getTimeZone("Canada/Mountain"); final long offset; final long hours; final String tzID; offset = currentTimeZone.getOffset(calendar.getTimeInMillis()); hours = -offset / (1000 * 60 * 60); tzID = "-0" + hours + ":00"; // assume single digit hour, we always are return dateFormat.format(calendar.getTime()) + 'T' + timeFormat.format(calendar.getTime()) + tzID; } }