Here you can find the source of getISO8601String(Date date)
public static String getISO8601String(Date date)
//package com.java2s; /*/* w ww . j a v a 2s. c o m*/ * File: NxNodeUtils.java * * Copyright (C) 2001, Ruth Mikkelson * * 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 library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu> * Department of Mathematics, Statistics and Computer Science * University of Wisconsin-Stout * Menomonie, WI 54751, USA * * This work was supported by the Intense Pulsed Neutron Source Division * of Argonne National Laboratory, Argonne, IL 60439-4845, USA. * * For further information, see <http://www.pns.anl.gov/ISAW/> * * Modified: * * $Log$ * Revision 1.25 2007/08/26 23:55:44 rmikk * Changed package name for the NeXus package * * Revision 1.24 2005/02/03 08:18:07 kramer * * Fixed an error in the getISO8601String(Date date) method, the current date * was used instead of 'date'. * * Commented out the section that writes fractions of a second because * parseISO8601(String dateString) can't read a ISO8601 string written to a * fraction of a second. * * Revision 1.23 2005/02/03 06:36:50 kramer * * Added the parseISO8601(String dateString) and getISO8601String(Date date) * methods which get a Date from an String written in ISO8601 format and a * String written in ISO8601 format from a Date. * * Revision 1.22 2004/12/23 18:47:05 rmikk * Fixed indentation and added lines between code. * * Revision 1.21 2004/05/14 15:03:27 rmikk * Removed unused variables * * Revision 1.20 2004/03/15 03:36:01 dennis * Moved view components, math and utils to new source tree * gov.anl.ipns.* * * Revision 1.19 2004/02/16 02:15:55 bouzekc * Removed unused import statements. * * Revision 1.18 2003/10/19 20:01:57 rmikk * Added some documentation * Fixed new units for time to be us * Fixed error in calculating factor with a numeric prefix * * Revision 1.17 2003/10/15 03:05:44 bouzekc * Fixed javadoc errors. * * Revision 1.16 2003/09/14 16:35:34 rmikk * -incorporated leading factors like 100us for times and * lengths * -Finished incorporating the time units up to hours. * * Revision 1.15 2003/06/18 20:32:54 pfpeterson * Removed deprecated method. * * Revision 1.14 2003/06/18 19:38:55 pfpeterson * ShowW() now calls method in StringUtil. * * Revision 1.13 2002/11/27 23:28:17 pfpeterson * standardized header * * Revision 1.12 2002/11/20 16:14:50 pfpeterson * reformating * * Revision 1.11 2002/06/19 16:27:04 rmikk * Eliminated commented out code. * Eliminated reference to deprecated Date.getYear() * Fixed code alignment and spacing * * Revision 1.10 2002/06/19 15:55:22 rmikk * Fixed the order of Date formats so that the seconds, when * there, is found * * Revision 1.9 2002/04/01 20:45:41 rmikk * Fixed Date Format exception report in jdk1.4 * Added some support for the Nexus NXChar type * * Revision 1.8 2002/02/26 15:46:41 rmikk * Fixed the utility Showw routine * Added a utility routine to getConversion factors * */ import java.text.DecimalFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { public static String getISO8601String(Date date) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); //one is added because Calendar.getInstance().get(Calendar.MONTH) = 0 // to refer to January but the ISO8601 standard uses 01 to represent // January int month = calendar.get(Calendar.MONTH) + 1; int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int milliseconds = calendar.get(Calendar.MILLISECOND); int zoneOffsetMs = calendar.get(Calendar.ZONE_OFFSET); DecimalFormat twoDigitFormat = new DecimalFormat("00"); DecimalFormat fourDigitFormat = new DecimalFormat("0000"); StringBuffer dateBuffer = new StringBuffer(); dateBuffer.append(fourDigitFormat.format(year)); dateBuffer.append("-"); dateBuffer.append(twoDigitFormat.format(month)); dateBuffer.append("-"); dateBuffer.append(twoDigitFormat.format(dayOfMonth)); dateBuffer.append("T"); dateBuffer.append(twoDigitFormat.format(hour)); dateBuffer.append(":"); dateBuffer.append(twoDigitFormat.format(minute)); if (second != 0 || milliseconds != 0) { dateBuffer.append(":"); dateBuffer.append(twoDigitFormat.format(second)); } /* * TODO Uncomment this when parseISO8601(String dateString) * can read a String written to the fraction of a second if (milliseconds != 0) { DecimalFormat threeDigitFormat = new DecimalFormat("000"); dateBuffer.append("."); dateBuffer.append(threeDigitFormat.format(milliseconds)); } */ if (zoneOffsetMs == 0) { dateBuffer.append("Z"); } else { //TODO Test if zoneOffsetMs>0 then +hh:mm should be used (and not // -hh:mm if (zoneOffsetMs > 0) dateBuffer.append("+"); else //then its negative dateBuffer.append("-"); int totalPosOffsetMs = Math.abs(zoneOffsetMs); int totalPosOffsetSec = totalPosOffsetMs / 1000; int totalPosOffsetMin = totalPosOffsetSec / 60; int hourToUse = totalPosOffsetMin / 60; int extraMins = totalPosOffsetMin - hourToUse * 60; dateBuffer.append(twoDigitFormat.format(hourToUse)); dateBuffer.append(":"); dateBuffer.append(twoDigitFormat.format(extraMins)); } return dateBuffer.toString(); } }