Java tutorial
//package com.java2s; /** * 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.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; public class Main { /** * The UTC time zone. Not sure if {@link TimeZone#getTimeZone(String)} * understands "UTC" in all environments, but it'll fall back to GMT * in such cases, which is in practice equivalent to UTC. */ public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); /** * Returns a ISO 8601 representation of the given date, which is * in an unknown timezone. This method is thread safe and non-blocking. * * @see <a href="https://issues.apache.org/jira/browse/TIKA-495">TIKA-495</a> * @param date given date * @return ISO 8601 date string, without timezone details */ public static String formatDateUnknownTimezone(Date date) { // Create the Calendar object in the system timezone Calendar calendar = GregorianCalendar.getInstance(TimeZone.getDefault(), Locale.US); calendar.setTime(date); // Have it formatted String formatted = formatDate(calendar); // Strip the timezone details before returning return formatted.substring(0, formatted.length() - 1); } /** * Returns a ISO 8601 representation of the given date. This method * is thread safe and non-blocking. * * @see <a href="https://issues.apache.org/jira/browse/TIKA-495">TIKA-495</a> * @param date given date * @return ISO 8601 date string, including timezone details */ public static String formatDate(Date date) { Calendar calendar = GregorianCalendar.getInstance(UTC, Locale.US); calendar.setTime(date); return doFormatDate(calendar); } /** * Returns a ISO 8601 representation of the given date. This method * is thread safe and non-blocking. * * @see <a href="https://issues.apache.org/jira/browse/TIKA-495">TIKA-495</a> * @param date given date * @return ISO 8601 date string, including timezone details */ public static String formatDate(Calendar date) { // Explicitly switch it into UTC before formatting date.setTimeZone(UTC); return doFormatDate(date); } private static String doFormatDate(Calendar calendar) { return String.format(Locale.ROOT, "%04d-%02d-%02dT%02d:%02d:%02dZ", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); } }